-
-
Save mfilipe/af1d9e3c29083fab5d608bc744415ae5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#0 0x00007fff00000001 in ?? () | |
#1 0x0000000000401434 in http_parser_execute (parser=0x609030, settings=0x7fffffffdc20, | |
data=0x7ffffffe4c10 "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nServer: Docker/1.10.2 (linux)\r\nDate: Mon, 07 Nov 2016 14:11:49 GMT\r\nTransfer-Encoding: chunked\r\n\r\n", len=147) at http_parser.c:777 | |
#2 0x0000000000400b1a in main (argc=1, argv=0x7fffffffde18) at testhttp.c:77 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <linux/un.h> | |
#include <limits.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include "http_parser.h" | |
#define VAR_RUN_DOCKER_SOCK "/var/run/docker.sock" | |
#define DOCKER_EVENTS "GET /events HTTP/1.1\r\n\r\n" | |
typedef struct { | |
int sock; | |
void* buffer; | |
int buf_len; | |
} custom_data_t; | |
// int get_chunk(http_parser* parser), const char *at, size_t length) { | |
// /* access to thread local custom_data_t struct. | |
// Use this access save parsed data for later use into thread local | |
// buffer, or communicate over socket | |
// */ | |
// fprintf(stdout, "HEADERS COMPLETED!\n");//, at); | |
// | |
// return 0; | |
// } | |
int main(int argc, char *argv[]) { | |
struct sockaddr_un address; | |
int fd; | |
size_t len = 100*1024;//, nparsed; | |
char buf[len]; | |
// char *buf = "Transfer-Encoding: chunked\r\n\r\n"; | |
// char *buf = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nServer: Docker/1.10.2 (linux)\r\nDate: Fri, 04 Nov 2016 19:24:09 GMT\r\nTransfer-Encoding: chunked\r\n\r\n\r\n"; | |
// size_t len = sizeof(buf), nparsed; | |
ssize_t recved; | |
custom_data_t *my_data = malloc(sizeof(custom_data_t)); | |
address.sun_family = AF_UNIX; | |
snprintf(address.sun_path,UNIX_PATH_MAX,VAR_RUN_DOCKER_SOCK); | |
if (!((fd = socket(PF_UNIX,SOCK_STREAM,0))>=0 && | |
connect(fd,(struct sockaddr*)&address,sizeof(struct sockaddr_un)) == 0)) { | |
fprintf(stderr,"error in socket/connect to %s: %s\n",VAR_RUN_DOCKER_SOCK,strerror(errno)); | |
return 1; | |
} | |
write(fd,DOCKER_EVENTS,strlen(DOCKER_EVENTS)); | |
http_parser_settings settings; | |
// settings.on_status = get_chunk; | |
// settings.on_message_complete = get_chunk; | |
http_parser *parser = malloc(sizeof(http_parser)); | |
http_parser_init(parser, HTTP_RESPONSE); | |
my_data->sock = fd; | |
parser->data = my_data; | |
while (1) { | |
recved = recv(fd, buf, len, 0); | |
if (recved < 0) { | |
fprintf(stderr,"error receiving msg from socket\n"); | |
return 1; | |
} | |
/* Start up / continue the parser. | |
* Note we pass recved==0 to signal that EOF has been received. | |
*/ | |
http_parser_execute(parser, &settings, buf, recved); | |
sleep(1); | |
} | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment