From 3b07b663a3791ef0f1b8e273259bb28faffc417e Mon Sep 17 00:00:00 2001 From: Joseph Hindin Date: Mon, 14 Mar 2016 21:38:37 +0200 Subject: [PATCH 1/2] first argument - port; some output on GET --- https-server.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/https-server.c b/https-server.c index 1dd6d0f..cd84b08 100644 --- a/https-server.c +++ b/https-server.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -66,6 +67,8 @@ #define O_RDONLY _O_RDONLY #endif +unsigned short serverPort = COMMON_HTTPS_PORT; + /* Instead of casting between these types, create a union with all of them, * to avoid -Wstrict-aliasing warnings. */ typedef union @@ -86,6 +89,15 @@ send_document_cb (struct evhttp_request *req, void *arg) const char *uri = evhttp_request_get_uri (req); struct evhttp_uri *decoded = NULL; + if (evhttp_request_get_command (req) == EVHTTP_REQ_GET) + { + struct evbuffer *buf = evbuffer_new(); + if (buf == NULL) return; + evbuffer_add_printf(buf, "Requested: %s\n", uri); + evhttp_send_reply(req, HTTP_OK, "OK", buf); + return; + } + /* We only handle POST requests. */ if (evhttp_request_get_command (req) != EVHTTP_REQ_POST) { evhttp_send_reply (req, 200, "OK", NULL); @@ -184,7 +196,6 @@ static int serve_some_http (void) struct evhttp *http; struct evhttp_bound_socket *handle; - unsigned short port = COMMON_HTTPS_PORT; #ifdef _WIN32 WSADATA WSAData; WSAStartup (0x101, &WSAData); @@ -230,9 +241,10 @@ static int serve_some_http (void) evhttp_set_gencb (http, send_document_cb, NULL); /* Now we tell the evhttp what port to listen on */ - handle = evhttp_bind_socket_with_handle (http, "0.0.0.0", port); + handle = evhttp_bind_socket_with_handle (http, "0.0.0.0", serverPort); if (! handle) - { fprintf (stderr, "couldn't bind to port %d. Exiting.\n", (int) port); + { fprintf (stderr, "couldn't bind to port %d. Exiting.\n", + (int) serverPort); return 1; } @@ -282,6 +294,25 @@ static int serve_some_http (void) int main (int argc, char **argv) { common_setup (); /* Initialize OpenSSL */ + if (argc > 1) { + char *end_ptr; + long lp = strtol(argv[1], &end_ptr, 0); + if (*end_ptr) { + fprintf(stderr, "Invalid integer\n"); + return -1; + } + if (lp <= 0) { + fprintf(stderr, "Port must be positive\n"); + return -1; + } + if (lp >= USHRT_MAX) { + fprintf(stderr, "Port must fit 16-bit range\n"); + return -1; + } + + serverPort = (unsigned short)lp; + } + /* now run http server (never returns) */ return serve_some_http (); } From 3deec04af03a41b831fabc78165661f347160849 Mon Sep 17 00:00:00 2001 From: Patrick Pelletier Date: Thu, 22 Nov 2018 23:09:45 -0800 Subject: [PATCH 2/2] Explain why this is not a good example. --- README.txt => README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) rename README.txt => README.md (53%) diff --git a/README.txt b/README.md similarity index 53% rename from README.txt rename to README.md index 0e0710c..89bbaaa 100644 --- a/README.txt +++ b/README.md @@ -1,3 +1,37 @@ +## This is probably not what you are looking for + +Understandably, given that the name of the repository is +`https-example`, I believe that a lot of people are coming here +expecting to find an example of how to write an https server and/or +client using [libevent](https://libevent.org/) and +[OpenSSL](https://www.openssl.org/). + +Instead, this was something I threw together in 2013 as an +example of a *problem* I was having when writing an https client and +server using libevent and OpenSSL. I only posted it so I could +[discuss it on the libevent mailing list](http://archives.seul.org/libevent/users/Jan-2013/msg00055.html). +I've kept it up in case it is useful to someone reading the mailing +list archives, and because a number of people have starred or forked +the repository. + +If you're looking for an https *client* using libevent and +OpenSSL, I would recommend checking out +[the example that comes with libevent](https://github.com/libevent/libevent/blob/master/sample/https-client.c). +If you're looking for an https *server* using libevent and OpenSSL, I +don't know of one to recommend. You might want to try asking on the +[libevent mailing list](http://archives.seul.org/libevent/users/). + +If you still want to look at the code in this repository, beware that +the client and server are written to work together. The server only +responds to a particular `POST` request generated by the client. +(This is because this example is a simplified version of some programs +I was writing at the time.) So if you want a general-purpose server, +you'll need to modify it to respond to `GET` requests, at the very +least. + +## Original README + +``` ppelletier@chives:~/src/https-example$ uname -a Linux chives 2.6.32-34-generic #77-Ubuntu SMP Tue Sep 13 19:39:17 UTC 2011 x86_64 GNU/Linux @@ -45,3 +79,4 @@ and libevent version "2.1.2-alpha-dev" code=0 POST failed socket error = Bad file descriptor (9) server said: (null) +```