Skip to content

Commit 6957193

Browse files
committed
Merge pull request mignon-p#2 from jhindin/master
first argument - port; some output on GET
2 parents fbedcb1 + 3b07b66 commit 6957193

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

https-server.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdio.h>
2020
#include <stdlib.h>
2121
#include <string.h>
22+
#include <limits.h>
2223

2324
#include <sys/types.h>
2425
#include <sys/stat.h>
@@ -66,6 +67,8 @@
6667
#define O_RDONLY _O_RDONLY
6768
#endif
6869

70+
unsigned short serverPort = COMMON_HTTPS_PORT;
71+
6972
/* Instead of casting between these types, create a union with all of them,
7073
* to avoid -Wstrict-aliasing warnings. */
7174
typedef union
@@ -86,6 +89,15 @@ send_document_cb (struct evhttp_request *req, void *arg)
8689
const char *uri = evhttp_request_get_uri (req);
8790
struct evhttp_uri *decoded = NULL;
8891

92+
if (evhttp_request_get_command (req) == EVHTTP_REQ_GET)
93+
{
94+
struct evbuffer *buf = evbuffer_new();
95+
if (buf == NULL) return;
96+
evbuffer_add_printf(buf, "Requested: %s\n", uri);
97+
evhttp_send_reply(req, HTTP_OK, "OK", buf);
98+
return;
99+
}
100+
89101
/* We only handle POST requests. */
90102
if (evhttp_request_get_command (req) != EVHTTP_REQ_POST)
91103
{ evhttp_send_reply (req, 200, "OK", NULL);
@@ -184,7 +196,6 @@ static int serve_some_http (void)
184196
struct evhttp *http;
185197
struct evhttp_bound_socket *handle;
186198

187-
unsigned short port = COMMON_HTTPS_PORT;
188199
#ifdef _WIN32
189200
WSADATA WSAData;
190201
WSAStartup (0x101, &WSAData);
@@ -230,9 +241,10 @@ static int serve_some_http (void)
230241
evhttp_set_gencb (http, send_document_cb, NULL);
231242

232243
/* Now we tell the evhttp what port to listen on */
233-
handle = evhttp_bind_socket_with_handle (http, "0.0.0.0", port);
244+
handle = evhttp_bind_socket_with_handle (http, "0.0.0.0", serverPort);
234245
if (! handle)
235-
{ fprintf (stderr, "couldn't bind to port %d. Exiting.\n", (int) port);
246+
{ fprintf (stderr, "couldn't bind to port %d. Exiting.\n",
247+
(int) serverPort);
236248
return 1;
237249
}
238250

@@ -282,6 +294,25 @@ static int serve_some_http (void)
282294
int main (int argc, char **argv)
283295
{ common_setup (); /* Initialize OpenSSL */
284296

297+
if (argc > 1) {
298+
char *end_ptr;
299+
long lp = strtol(argv[1], &end_ptr, 0);
300+
if (*end_ptr) {
301+
fprintf(stderr, "Invalid integer\n");
302+
return -1;
303+
}
304+
if (lp <= 0) {
305+
fprintf(stderr, "Port must be positive\n");
306+
return -1;
307+
}
308+
if (lp >= USHRT_MAX) {
309+
fprintf(stderr, "Port must fit 16-bit range\n");
310+
return -1;
311+
}
312+
313+
serverPort = (unsigned short)lp;
314+
}
315+
285316
/* now run http server (never returns) */
286317
return serve_some_http ();
287318
}

0 commit comments

Comments
 (0)