bessarabov
bessarabov

Reputation: 11871

How to work with http web server via file socket in perl?

It is possible to work with web server living in file socket with linux commands:

# /bin/echo -e "GET /containers/json?all=1 HTTP/1.0\r\n" | nc -U /var/run/docker.sock ; echo ''
HTTP/1.0 200 OK
Content-Type: application/json
Date: Sun, 03 Jan 2016 23:31:54 GMT
Content-Length: 2

[]
#

How can I do the same thing using Perl modules? I would prefer to do the same thing with HTTP::Tiny, but I can't figure out how to use it with file socket.

For now I'm just using perl system() but I want to use HTTP::Tiny to make code simpler.

Upvotes: 4

Views: 1314

Answers (2)

bluefeet
bluefeet

Reputation: 391

HTTP::Tiny::UNIX will do what you want. From the docs:

This is a subclass of HTTP::Tiny to connect to HTTP server over Unix socket.

I'm using Starman to spawn a unix-socket only HTTP daemon which I connect to with an HTTP::Tiny::UNIX client. Works great.

Upvotes: 0

ppp
ppp

Reputation: 522

I couldn't find a way to make HTTP::Tiny use UNIX sockets but I did find a way to make LWP work:

use strict;
use warnings;
use LWP::UserAgent;
use LWP::Protocol::http::SocketUnixAlt;

my $socket = "/var/run/docker.sock";
my $geturi = "/containers/json?all=1"; #GET

LWP::Protocol::implementor( http => 'LWP::Protocol::http::SocketUnixAlt' );

my $ua = LWP::UserAgent->new();
my $res = $ua->get("http:/var/run/docker.sock/"."$geturi");
print $res->content;

There's also a couple of docker modules in CPAN if you think that manually doing HTTP requests is too much of a hassle and want another abstraction layer.

Upvotes: 3

Related Questions