libevent(六)http server
客户端:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <unistd.h> #include <event2/event.h> #include <event2/http.h> #include <event2/http_struct.h> #include <event2/bufferevent.h> #include <event2/buffer.h> #define DEFAULT_URL "http://127.0.0.1:8080" void http_request_done(struct evhttp_request *req, void *arg){ char buffer[256]; int nread; if (req == NULL) { printf("request failed\n"); return; } fprintf(stderr, "Response line: %d %s\n", evhttp_request_get_response_code(req),req->response_code_line); while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req), buffer, sizeof(buffer))) > 0) { fwrite(buffer, nread, 1, stdout); } } int main(int argc, char **argv){ char *url = DEFAULT_URL; if (argc == 2) { url = argv[1]; } struct evhttp_uri *http_uri = NULL; const char *scheme, *host, *path, *query; char uri[256]; int port; http_uri = evhttp_uri_parse(url); scheme = evhttp_uri_get_scheme(http_uri); if (scheme == NULL || (strcasecmp(scheme, "https") != 0 && strcasecmp(scheme, "http") != 0)) { printf("url must be http or https\n"); return 1; } host = evhttp_uri_get_host(http_uri); port = evhttp_uri_get_port(http_uri); if (port == -1) { port = (strcasecmp(scheme, "http") == 0) ? 80 : 443; } path = evhttp_uri_get_path(http_uri); if (strlen(path) == 0) { path = "/"; } query = evhttp_uri_get_query(http_uri); if (query == NULL) { snprintf(uri, sizeof(uri) - 1, "%s", path); } else { snprintf(uri, sizeof(uri) - 1, "%s?%s", path, query); } uri[sizeof(uri) - 1] = '\0'; printf("url: %s\n", url); printf("scheme: %s\n", scheme); printf("host: %s\n", host); printf("port: %d\n", port); printf("path: %s\n", path); printf("uri: %s\n", uri); struct event_base *base; struct evhttp_connection *conn; struct evhttp_request *req; base = event_base_new(); conn = evhttp_connection_base_new(base, NULL, host, port); req = evhttp_request_new(http_request_done, base); evhttp_add_header(req->output_headers, "Host", host); //evhttp_add_header(req->output_headers, "Connection", "close"); evhttp_make_request(conn, req, EVHTTP_REQ_GET, uri); event_base_dispatch(base); return 0; }
服务器:
#include <stdio.h> #include <stdlib.h> #include <event2/keyvalq_struct.h> #include <event2/http.h> #include <event2/event.h> #include <event2/buffer.h> #define DEFAULT_PORT 8080 void http_request_cb(struct evhttp_request *req, void *arg); void print_request(struct evhttp_request *req, void *arg); void php_request_cb(struct evhttp_request *req, void *arg); int main(int argc, char **argv) { int port = DEFAULT_PORT; if (argc == 2) { port = atoi(argv[1]); } struct event_base *base = event_base_new(); struct evhttp *http = evhttp_new(base); evhttp_set_gencb(http, http_request_cb, NULL); evhttp_set_cb(http, "/index.php", php_request_cb, NULL); evhttp_bind_socket(http, "0.0.0.0", port); event_base_dispatch(base); return 0; } void php_request_cb(struct evhttp_request *req, void *arg) { printf("run php_request_cb...\n"); } void http_request_cb(struct evhttp_request *req, void *arg) { printf("run http_request_cb...\n"); print_request(req, arg); const char *uri = evhttp_request_get_uri(req); printf("uri: %s\n", uri); const char *decoded_uri = evhttp_decode_uri(uri); printf("decoded_uri: %s\n", decoded_uri); struct evhttp_uri *decoded = NULL; const char *path; /* Decode the URI */ decoded = evhttp_uri_parse(uri); if (!decoded) { printf("It's not a good URI. Sending BADREQUEST\n"); evhttp_send_error(req, HTTP_BADREQUEST, 0); return; } /* Let's see what path the user asked for. */ path = evhttp_uri_get_path(decoded); if (!path) path = "/"; printf("path: %s\n", path); struct evbuffer *evb = evbuffer_new(); evbuffer_add_printf(evb, "Server response"); evhttp_send_reply(req, HTTP_OK, "OK", evb); evbuffer_free(evb); } void print_request(struct evhttp_request *req, void *arg) { const char *cmdtype; struct evkeyvalq *headers; struct evkeyval *header; struct evbuffer *buf; switch (evhttp_request_get_command(req)) { case EVHTTP_REQ_GET: cmdtype = "GET"; break; case EVHTTP_REQ_POST: cmdtype = "POST"; break; case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break; case EVHTTP_REQ_PUT: cmdtype = "PUT"; break; case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break; case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break; case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break; case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break; case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break; default: cmdtype = "unknown"; break; } printf("Received a %s request for %s\nHeaders:\n", cmdtype, evhttp_request_get_uri(req)); headers = evhttp_request_get_input_headers(req); for (header = headers->tqh_first; header; header = header->next.tqe_next) { printf(" %s: %s\n", header->key, header->value); } buf = evhttp_request_get_input_buffer(req); puts("Input data: <<<"); while (evbuffer_get_length(buf)) { int n; char cbuf[128]; n = evbuffer_remove(buf, cbuf, sizeof(cbuf)); if (n > 0) (void) fwrite(cbuf, 1, n, stdout); } puts(">>>"); }