客户端:
#include
#include
#include <string.h>
#include
#include
#include
#include
#include
#include
}int main(int argc, char **argv){char *url &#61; DEFAULT_URL;if (argc &#61;&#61; 2) {url &#61; argv[1];}struct evhttp_uri *http_uri &#61; NULL;const char *scheme, *host, *path, *query;char uri[256];int port;http_uri &#61; evhttp_uri_parse(url);scheme &#61; evhttp_uri_get_scheme(http_uri);if (scheme &#61;&#61; NULL || (strcasecmp(scheme, "https") !&#61; 0 &&strcasecmp(scheme, "http") !&#61; 0)) {printf("url must be http or https\n");return 1;}host &#61; evhttp_uri_get_host(http_uri);port &#61; evhttp_uri_get_port(http_uri);if (port &#61;&#61; -1) {port &#61; (strcasecmp(scheme, "http") &#61;&#61; 0) ? 80 : 443;}path &#61; evhttp_uri_get_path(http_uri);if (strlen(path) &#61;&#61; 0) {path &#61; "/";}query &#61; evhttp_uri_get_query(http_uri);if (query &#61;&#61; NULL) {snprintf(uri, sizeof(uri) - 1, "%s", path);} else {snprintf(uri, sizeof(uri) - 1, "%s?%s", path, query);}uri[sizeof(uri) - 1] &#61; &#39;\0&#39;;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 &#61; event_base_new();conn &#61; evhttp_connection_base_new(base, NULL, host, port);req &#61; 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;
}
服务器&#xff1a;
#include
#include
#include
#include
#include
#include
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 &#61; DEFAULT_PORT;if (argc &#61;&#61; 2) {port &#61; atoi(argv[1]);}struct event_base *base &#61; event_base_new();struct evhttp *http &#61; 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 &#61; evhttp_request_get_uri(req);printf("uri: %s\n", uri);const char *decoded_uri &#61; evhttp_decode_uri(uri);printf("decoded_uri: %s\n", decoded_uri);struct evhttp_uri *decoded &#61; NULL;const char *path;/* Decode the URI */decoded &#61; evhttp_uri_parse(uri);if (!decoded) {printf("It&#39;s not a good URI. Sending BADREQUEST\n");evhttp_send_error(req, HTTP_BADREQUEST, 0);return;}/* Let&#39;s see what path the user asked for. */path &#61; evhttp_uri_get_path(decoded);if (!path) path &#61; "/";printf("path: %s\n", path);struct evbuffer *evb &#61; 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 &#61; "GET"; break;case EVHTTP_REQ_POST: cmdtype &#61; "POST"; break;case EVHTTP_REQ_HEAD: cmdtype &#61; "HEAD"; break;case EVHTTP_REQ_PUT: cmdtype &#61; "PUT"; break;case EVHTTP_REQ_DELETE: cmdtype &#61; "DELETE"; break;case EVHTTP_REQ_OPTIONS: cmdtype &#61; "OPTIONS"; break;case EVHTTP_REQ_TRACE: cmdtype &#61; "TRACE"; break;case EVHTTP_REQ_CONNECT: cmdtype &#61; "CONNECT"; break;case EVHTTP_REQ_PATCH: cmdtype &#61; "PATCH"; break;default: cmdtype &#61; "unknown"; break;}printf("Received a %s request for %s\nHeaders:\n",cmdtype, evhttp_request_get_uri(req));headers &#61; evhttp_request_get_input_headers(req);for (header &#61; headers->tqh_first; header;header &#61; header->next.tqe_next) {printf(" %s: %s\n", header->key, header->value);}buf &#61; evhttp_request_get_input_buffer(req);puts("Input data: <<<");while (evbuffer_get_length(buf)) {int n;char cbuf[128];n &#61; evbuffer_remove(buf, cbuf, sizeof(cbuf));if (n > 0)(void) fwrite(cbuf, 1, n, stdout);}puts(">>>");
}