Skip to content

Instantly share code, notes, and snippets.

@errzey
Last active August 29, 2015 14:01
Show Gist options
  • Save errzey/d2478419fec05d1f61f7 to your computer and use it in GitHub Desktop.
Save errzey/d2478419fec05d1f61f7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <inttypes.h>
#include <evhtp.h>
static evhtp_res print_data(evhtp_request_t * req, struct evbuffer * buf, void * arg);
static evhtp_res print_new_chunk_len(evhtp_request_t * req, uint64_t len, void * arg);
static evhtp_res print_chunks_complete(evhtp_request_t * req, void * arg);
static evhtp_res print_chunk_complete(evhtp_request_t * req, void * arg);
static void
request_cb(evhtp_request_t * req, void * arg) {
struct event_base * evbase = req->conn->evbase;
evhtp_connection_t * conn = req->conn;
evhtp_request_t * n_req = evhtp_request_new(request_cb, evbase);
evhtp_set_hook(&n_req->hooks, evhtp_hook_on_read, print_data, evbase);
evhtp_set_hook(&n_req->hooks, evhtp_hook_on_new_chunk, print_new_chunk_len, NULL);
evhtp_set_hook(&n_req->hooks, evhtp_hook_on_chunk_complete, print_chunk_complete, NULL);
evhtp_set_hook(&n_req->hooks, evhtp_hook_on_chunks_complete, print_chunks_complete, NULL);
evhtp_headers_add_header(n_req->headers_out, evhtp_header_new("Host", "ieatfood.net", 0, 0));
evhtp_headers_add_header(n_req->headers_out, evhtp_header_new("User-Agent", "libevhtp", 0, 0));
evhtp_headers_add_header(n_req->headers_out, evhtp_header_new("Connection", "close", 0, 0));
evhtp_make_request(conn, n_req, htp_method_GET, "/");
printf("hi %zu\n", evbuffer_get_length(req->buffer_in));
}
static evhtp_res
print_data(evhtp_request_t * req, evbuf_t * buf, void * arg) {
printf("Got %zu bytes\n", evbuffer_get_length(buf));
return EVHTP_RES_OK;
}
static evhtp_res
print_new_chunk_len(evhtp_request_t * req, uint64_t len, void * arg) {
printf("started new chunk, %" PRIu64 " bytes\n", len);
return EVHTP_RES_OK;
}
static evhtp_res
print_chunk_complete(evhtp_request_t * req, void * arg) {
printf("ended a single chunk\n");
return EVHTP_RES_OK;
}
static evhtp_res
print_chunks_complete(evhtp_request_t * req, void * arg) {
printf("all chunks read\n");
return EVHTP_RES_OK;
}
int
main(int argc, char ** argv) {
evbase_t * evbase;
evhtp_connection_t * conn;
evhtp_request_t * request;
evbase = event_base_new();
conn = evhtp_connection_new(evbase, "75.126.169.52", 80);
request = evhtp_request_new(request_cb, evbase);
evhtp_set_hook(&request->hooks, evhtp_hook_on_read, print_data, evbase);
evhtp_set_hook(&request->hooks, evhtp_hook_on_new_chunk, print_new_chunk_len, NULL);
evhtp_set_hook(&request->hooks, evhtp_hook_on_chunk_complete, print_chunk_complete, NULL);
evhtp_set_hook(&request->hooks, evhtp_hook_on_chunks_complete, print_chunks_complete, NULL);
evhtp_headers_add_header(request->headers_out, evhtp_header_new("Host", "ieatfood.net", 0, 0));
evhtp_headers_add_header(request->headers_out, evhtp_header_new("User-Agent", "libevhtp", 0, 0));
evhtp_headers_add_header(request->headers_out, evhtp_header_new("Connection", "keep-alive", 0, 0));
evhtp_make_request(conn, request, htp_method_GET, "/");
event_base_loop(evbase, 0);
event_base_free(evbase);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <inttypes.h>
#include <evhtp.h>
static evhtp_res print_data(evhtp_request_t * req, struct evbuffer * buf, void * arg);
static evhtp_res print_new_chunk_len(evhtp_request_t * req, uint64_t len, void * arg);
static evhtp_res print_chunks_complete(evhtp_request_t * req, void * arg);
static evhtp_res print_chunk_complete(evhtp_request_t * req, void * arg);
static void
request_cb(evhtp_request_t * req, void * arg) {
struct event_base * evbase = req->conn->evbase;
evhtp_connection_t * conn = req->conn;
evhtp_request_t * n_req = evhtp_request_new(request_cb, evbase);
evhtp_set_hook(&n_req->hooks, evhtp_hook_on_read, print_data, evbase);
evhtp_set_hook(&n_req->hooks, evhtp_hook_on_new_chunk, print_new_chunk_len, NULL);
evhtp_set_hook(&n_req->hooks, evhtp_hook_on_chunk_complete, print_chunk_complete, NULL);
evhtp_set_hook(&n_req->hooks, evhtp_hook_on_chunks_complete, print_chunks_complete, NULL);
evhtp_headers_add_header(n_req->headers_out, evhtp_header_new("Host", "ieatfood.net", 0, 0));
evhtp_headers_add_header(n_req->headers_out, evhtp_header_new("User-Agent", "libevhtp", 0, 0));
evhtp_headers_add_header(n_req->headers_out, evhtp_header_new("Connection", "close", 0, 0));
evhtp_make_request(conn, n_req, htp_method_GET, "/");
printf("hi %zu\n", evbuffer_get_length(req->buffer_in));
}
static evhtp_res
print_data(evhtp_request_t * req, evbuf_t * buf, void * arg) {
printf("Got %zu bytes\n", evbuffer_get_length(buf));
return EVHTP_RES_OK;
}
static evhtp_res
print_new_chunk_len(evhtp_request_t * req, uint64_t len, void * arg) {
printf("started new chunk, %" PRIu64 " bytes\n", len);
return EVHTP_RES_OK;
}
static evhtp_res
print_chunk_complete(evhtp_request_t * req, void * arg) {
printf("ended a single chunk\n");
return EVHTP_RES_OK;
}
static evhtp_res
print_chunks_complete(evhtp_request_t * req, void * arg) {
printf("all chunks read\n");
return EVHTP_RES_OK;
}
static void
print_events(evhtp_connection_t * conn, short events, void * arg) {
printf("%X\n", events);
}
int
main(int argc, char ** argv) {
evbase_t * evbase;
evhtp_connection_t * conn;
evhtp_request_t * request;
evbase = event_base_new();
conn = evhtp_connection_new(evbase, "75.126.169.52", 80);
request = evhtp_request_new(request_cb, evbase);
evhtp_set_hook(&request->hooks, evhtp_hook_on_read, print_data, evbase);
evhtp_set_hook(&request->hooks, evhtp_hook_on_new_chunk, print_new_chunk_len, NULL);
evhtp_set_hook(&request->hooks, evhtp_hook_on_chunk_complete, print_chunk_complete, NULL);
evhtp_set_hook(&request->hooks, evhtp_hook_on_chunks_complete, print_chunks_complete, NULL);
evhtp_set_hook(&conn->hooks, evhtp_hook_on_event, print_events, NULL);
evhtp_headers_add_header(request->headers_out, evhtp_header_new("Host", "ieatfood.net", 0, 0));
evhtp_headers_add_header(request->headers_out, evhtp_header_new("User-Agent", "libevhtp", 0, 0));
evhtp_headers_add_header(request->headers_out, evhtp_header_new("Connection", "keep-alive", 0, 0));
evhtp_make_request(conn, request, htp_method_GET, "/");
event_base_loop(evbase, 0);
event_base_free(evbase);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <inttypes.h>
#include <evhtp.h>
static evhtp_res print_data(evhtp_req_t * req, struct evbuffer * buf, void * arg);
static evhtp_res print_new_chunk_len(evhtp_req_t * req, uint64_t len, void * arg);
static evhtp_res print_chunks_complete(evhtp_req_t * req, void * arg);
static evhtp_res print_chunk_complete(evhtp_req_t * req, void * arg);
static void
req_cb(evhtp_req_t * req, void * arg) {
struct event_base * evbase = evhtp_req_get_evbase(req);
evhtp_conn_t * conn = evhtp_req_get_conn(req);
evhtp_req_t * n_req = evhtp_req_new(req_cb, evbase);
evhtp_hdrs_t * headers = evhtp_req_get_headers_out(n_req);
evhtp_req_set_hook(n_req, evhtp_hook_on_read, print_data, evbase);
evhtp_req_set_hook(n_req, evhtp_hook_on_new_chunk, print_new_chunk_len, NULL);
evhtp_req_set_hook(n_req, evhtp_hook_on_chunk_complete, print_chunk_complete, NULL);
evhtp_req_set_hook(n_req, evhtp_hook_on_chunks_complete, print_chunks_complete, NULL);
evhtp_hdrs_add_header(headers, evhtp_hdr_new("Host", "ieatfood.net", 0, 0));
evhtp_hdrs_add_header(headers, evhtp_hdr_new("User-Agent", "libevhtp", 0, 0));
evhtp_hdrs_add_header(headers, evhtp_hdr_new("Connection", "close", 0, 0));
evhtp_make_req(conn, n_req, evhtp_method_GET, "/");
printf("hi %zu\n", evbuffer_get_length(evhtp_req_buffer_in(req)));
}
static evhtp_res
print_data(evhtp_req_t * req, struct evbuffer * buf, void * arg) {
printf("Got %zu bytes\n", evbuffer_get_length(buf));
return EVHTP_RES_OK;
}
static evhtp_res
print_new_chunk_len(evhtp_req_t * req, uint64_t len, void * arg) {
printf("started new chunk, %" PRIu64 " bytes\n", len);
return EVHTP_RES_OK;
}
static evhtp_res
print_chunk_complete(evhtp_req_t * req, void * arg) {
printf("ended a single chunk\n");
return EVHTP_RES_OK;
}
static evhtp_res
print_chunks_complete(evhtp_req_t * req, void * arg) {
printf("all chunks read\n");
return EVHTP_RES_OK;
}
int
main(int argc, char ** argv) {
struct event_base * evbase;
evhtp_conn_t * conn;
evhtp_req_t * req;
evhtp_hdrs_t * headers;
evbase = event_base_new();
conn = evhtp_conn_new(evbase, "75.126.169.52", 80);
req = evhtp_req_new(req_cb, evbase);
headers = evhtp_req_get_headers_out(req);
evhtp_req_set_hook(req, evhtp_hook_on_read, print_data, evbase);
evhtp_req_set_hook(req, evhtp_hook_on_new_chunk, print_new_chunk_len, NULL);
evhtp_req_set_hook(req, evhtp_hook_on_chunk_complete, print_chunk_complete, NULL);
evhtp_req_set_hook(req, evhtp_hook_on_chunks_complete, print_chunks_complete, NULL);
evhtp_hdrs_add_header(headers, evhtp_hdr_new("Host", "ieatfood.net", 0, 0));
evhtp_hdrs_add_header(headers, evhtp_hdr_new("User-Agent", "libevhtp", 0, 0));
evhtp_hdrs_add_header(headers, evhtp_hdr_new("Connection", "keep-alive", 0, 0));
evhtp_make_req(conn, req, evhtp_method_GET, "/");
event_base_loop(evbase, 0);
event_base_free(evbase);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment