Skip to content

Instantly share code, notes, and snippets.

@mythosil
Created August 18, 2011 15:44
Show Gist options
  • Save mythosil/1154359 to your computer and use it in GitHub Desktop.
Save mythosil/1154359 to your computer and use it in GitHub Desktop.
evhttp returns text/plain only
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <event.h>
#include <evhttp.h>
#define HTTPD_ADDR "0.0.0.0"
#define HTTPD_PORT 8080
void req_handler(struct evhttp_request *r, void *arg)
{
struct evbuffer *evbuf;
char message[] = "Hello World";
int message_length = strlen(message);
char content_length[8];
memset(content_length, 0, 8);
snprintf(content_length, 7, "%d", message_length);
evhttp_add_header(r->output_headers, "Content-Type", "text/plain");
evhttp_add_header(r->output_headers, "Content-Length", content_length);
evbuf = evbuffer_new();
if (evbuf == NULL) {
evhttp_send_error(r, HTTP_SERVUNAVAIL, "failed to create buffer");
return;
}
evbuffer_add(evbuf, message, message_length);
evhttp_send_reply(r, HTTP_OK, "", evbuf);
evbuffer_free(evbuf);
}
int main(int argc, const char* argv[])
{
struct event_base *ev_base;
struct evhttp *httpd;
ev_base = event_base_new();
httpd = evhttp_new(ev_base);
if (evhttp_bind_socket(httpd, HTTPD_ADDR, HTTPD_PORT) < 0) {
perror("evhttp_bind_socket()");
exit(EXIT_FAILURE);
}
evhttp_set_gencb(httpd, req_handler, NULL);
event_base_dispatch(ev_base);
evhttp_free(httpd);
event_base_free(ev_base);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment