5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2006 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "silchttpserver.h"
23 /************************** Types and definitions ***************************/
25 #define SILC_HTTP_SERVER_TIMEOUT 120 /* Connection timeout */
26 #define SILC_HTTP_SERVER_CONNS 2 /* Default number of connections */
27 #define SILC_HTTP_SERVER_BUFLEN 1024 /* Default data buffer length */
28 #define SILC_HTTP_SERVER_HEADER "HTTP/1.1 200 OK\r\nServer: SILCHTTP/1.0\r\n"
30 /* HTTP server context */
31 struct SilcHttpServerStruct {
32 SilcNetListener listener; /* Server listener */
33 SilcSchedule schedule; /* Scheduler */
34 SilcList allconns; /* All connections */
35 SilcList conns; /* Connection free list */
36 SilcHttpServerCallback callback; /* Requset callback */
37 void *context; /* Request callback context */
40 /* HTTP connection context */
41 struct SilcHttpConnectionStruct {
42 struct SilcHttpConnectionStruct *next;
43 struct SilcHttpConnectionStruct *next2;
44 SilcHttpServer httpd; /* Server */
45 SilcStream stream; /* Connection stream */
46 SilcBuffer inbuf; /* Read data buffer */
47 SilcBuffer outbuf; /* Write data buffer */
48 SilcInt64 touched; /* Time last connection was touched */
49 SilcMime curheaders; /* HTTP request headers */
50 SilcMime headers; /* HTTP reply headers */
51 unsigned char *hptr; /* Pointer to start of headers */
52 char *method; /* Method */
54 unsigned int keepalive : 1; /* Keep alive */
57 /************************ Static utility functions **************************/
59 /* Close HTTP connection */
61 static void silc_http_server_close_connection(SilcHttpConnection conn)
64 silc_mime_free(conn->headers);
67 if (conn->curheaders) {
68 silc_mime_free(conn->curheaders);
69 conn->curheaders = NULL;
71 silc_buffer_clear(conn->inbuf);
72 silc_buffer_clear(conn->outbuf);
73 silc_buffer_reset(conn->inbuf);
74 silc_buffer_reset(conn->outbuf);
75 conn->hptr = conn->method = conn->uri = NULL;
80 SILC_LOG_DEBUG(("Closing HTTP connection %p", conn));
82 silc_schedule_task_del_by_context(conn->httpd->schedule, conn);
83 silc_stream_set_notifier(conn->stream, conn->httpd->schedule, NULL, NULL);
84 silc_stream_destroy(conn->stream);
87 /* Add to free list */
88 silc_list_add(conn->httpd->conns, conn);
93 static SilcBool silc_http_server_parse(SilcHttpServer httpd,
94 SilcHttpConnection conn)
96 SilcUInt32 data_len, cll;
97 unsigned char *data, *tmp;
98 const char *value, *cl;
99 SilcBufferStruct postdata;
102 SILC_LOG_DEBUG(("Parsing HTTP data"));
104 data = silc_buffer_data(conn->inbuf);
105 data_len = silc_buffer_len(conn->inbuf);
107 /* Check for end of headers */
108 for (i = 0; i < data_len ; i++) {
109 if (data_len - i >= 4 &&
110 data[i ] == '\r' && data[i + 1] == '\n' &&
111 data[i + 2] == '\r' && data[i + 3] == '\n')
117 SILC_LOG_HEXDUMP(("HTTP data"), silc_buffer_data(conn->inbuf),
118 silc_buffer_len(conn->inbuf));
120 if (!conn->method && !conn->uri) {
121 tmp = memchr(data, '\n', data_len);
122 if (!tmp || tmp[-1] != '\r') {
123 if (data_len < SILC_HTTP_SERVER_BUFLEN)
130 if (strchr(data, ' '))
131 *strchr(data, ' ') = 0;
133 SILC_LOG_DEBUG(("Method: '%s'", conn->method));
136 tmp = memchr(data, '\0', data_len);
138 if (data_len < SILC_HTTP_SERVER_BUFLEN)
143 if (strchr(tmp, ' '))
144 *strchr(tmp, ' ') = 0;
146 SILC_LOG_DEBUG(("URI: '%s'", conn->uri));
148 /* Protocol version compatibility */
149 tmp = ((unsigned char *)memchr(tmp, '\0', data_len - (tmp - data))) + 1;
150 SILC_LOG_DEBUG(("Protocol: %s", tmp));
151 if (strstr(tmp, "HTTP/1.0"))
152 conn->keepalive = FALSE;
153 if (strstr(tmp, "HTTP/1.1"))
154 conn->keepalive = TRUE;
155 if (strstr(tmp, "HTTP/1.2"))
156 conn->keepalive = TRUE;
158 /* Get HTTP headers */
159 tmp = memchr(tmp, '\0', data_len - (tmp - data));
161 if (data_len < SILC_HTTP_SERVER_BUFLEN)
165 if (data_len - (tmp - data) < 2) {
166 if (data_len < SILC_HTTP_SERVER_BUFLEN)
173 /* Parse headers and data area */
174 conn->curheaders = silc_mime_decode(NULL, conn->hptr,
175 data_len - (conn->hptr - data));
176 if (!conn->curheaders)
179 /* Check for persistent connection */
180 value = silc_mime_get_field(conn->curheaders, "Connection");
181 if (value && !strcasecmp(value, "close"))
182 conn->keepalive = FALSE;
184 /* Deliver request to caller */
185 if (!strcasecmp(conn->method, "GET") || !strcasecmp(conn->method, "HEAD")) {
186 httpd->callback(httpd, conn, conn->uri, conn->method,
187 NULL, httpd->context);
189 } else if (!strcasecmp(conn->method, "POST")) {
191 tmp = (unsigned char *)silc_mime_get_data(conn->curheaders, &data_len);
195 /* Check we have received all data */
196 cl = silc_mime_get_field(conn->curheaders, "Content-Length");
197 if (cl && sscanf(cl, "%lu", (unsigned long *)&cll) == 1) {
198 if (data_len < cll) {
199 /* More data to come */
200 silc_mime_free(conn->curheaders);
201 conn->curheaders = NULL;
206 silc_buffer_set(&postdata, tmp, data_len);
207 SILC_LOG_HEXDUMP(("HTTP POST data"), tmp, data_len);
209 httpd->callback(httpd, conn, conn->uri, conn->method,
210 &postdata, httpd->context);
212 /* Send bad request */
213 silc_http_server_send_error(httpd, conn, "400 Bad Request",
214 "<body><h1>400 Bad Request</h1><body>");
221 /* Send HTTP data to connection */
223 static SilcBool silc_http_server_send_internal(SilcHttpServer httpd,
224 SilcHttpConnection conn,
230 SILC_LOG_HEXDUMP(("HTTP data"), silc_buffer_data(data),
231 silc_buffer_len(data));
233 /* Write the packet to the stream */
234 while (silc_buffer_len(data) > 0) {
235 ret = silc_stream_write(conn->stream, silc_buffer_data(data),
236 silc_buffer_len(data));
237 if (ret == 0 || ret == - 2)
241 /* Cannot write now, write later. */
242 if (silc_buffer_len(data) - ret >= silc_buffer_taillen(conn->outbuf))
243 if (!silc_buffer_realloc(conn->outbuf,
244 silc_buffer_truelen(conn->outbuf) +
245 silc_buffer_len(data) - ret)) {
246 conn->keepalive = FALSE;
247 silc_http_server_close_connection(conn);
250 silc_buffer_pull_tail(conn->outbuf, silc_buffer_len(data) - ret);
251 silc_buffer_put(conn->outbuf, silc_buffer_data(data) + ret,
252 silc_buffer_len(data) - ret);
257 silc_buffer_pull(data, ret);
261 /* Data sent, close connection */
262 SILC_LOG_DEBUG(("Data sent %p", conn));
263 silc_http_server_close_connection(conn);
269 /* Allocate connection context */
271 static SilcHttpConnection silc_http_server_alloc_connection(void)
273 SilcHttpConnection conn;
275 conn = silc_calloc(1, sizeof(*conn));
279 conn->inbuf = silc_buffer_alloc(SILC_HTTP_SERVER_BUFLEN);
285 conn->outbuf = silc_buffer_alloc(SILC_HTTP_SERVER_BUFLEN);
287 silc_buffer_free(conn->inbuf);
292 silc_buffer_reset(conn->inbuf);
293 silc_buffer_reset(conn->outbuf);
298 /* Check if connection has timedout */
300 SILC_TASK_CALLBACK(silc_http_server_connection_timeout)
302 SilcHttpConnection conn = context;
303 SilcInt64 curtime = silc_time();
305 if (curtime - conn->touched > SILC_HTTP_SERVER_TIMEOUT) {
306 SILC_LOG_DEBUG(("Connection timeout %p", conn));
307 conn->keepalive = FALSE;
308 silc_http_server_close_connection(conn);
312 silc_schedule_task_add_timeout(conn->httpd->schedule,
313 silc_http_server_connection_timeout, conn,
314 SILC_HTTP_SERVER_TIMEOUT, 0);
317 /* Data I/O callback */
319 static void silc_http_server_io(SilcStream stream, SilcStreamStatus status,
322 SilcHttpConnection conn = context;
323 SilcHttpServer httpd = conn->httpd;
327 case SILC_STREAM_CAN_READ:
328 SILC_LOG_DEBUG(("Read HTTP data %p", conn));
330 conn->touched = silc_time();
332 /* Make sure we have fair amount of free space in inbuf */
333 if (silc_buffer_taillen(conn->inbuf) < SILC_HTTP_SERVER_BUFLEN)
334 if (!silc_buffer_realloc(conn->inbuf, silc_buffer_truelen(conn->inbuf) +
335 SILC_HTTP_SERVER_BUFLEN * 2)) {
336 conn->keepalive = FALSE;
337 silc_http_server_close_connection(conn);
341 /* Read data from stream */
342 ret = silc_stream_read(conn->stream, conn->inbuf->tail,
343 silc_buffer_taillen(conn->inbuf));
345 if (ret == 0 || ret == -2) {
346 conn->keepalive = FALSE;
347 silc_http_server_close_connection(conn);
352 /* Cannot read now, do it later. */
353 silc_buffer_pull(conn->inbuf, silc_buffer_len(conn->inbuf));
357 SILC_LOG_DEBUG(("Read %d bytes data", ret));
360 silc_buffer_pull_tail(conn->inbuf, ret);
361 if (!silc_http_server_parse(httpd, conn)) {
362 conn->keepalive = FALSE;
363 silc_http_server_close_connection(conn);
368 case SILC_STREAM_CAN_WRITE:
369 SILC_LOG_DEBUG(("Write HTTP data %p", conn));
371 conn->touched = silc_time();
373 /* Write pending data to stream */
374 while (silc_buffer_len(conn->outbuf) > 0) {
375 ret = silc_stream_write(conn->stream, silc_buffer_data(conn->outbuf),
376 silc_buffer_len(conn->outbuf));
378 if (ret == 0 || ret == -2) {
379 conn->keepalive = FALSE;
380 silc_http_server_close_connection(conn);
385 /* Cannot write now, write later. */
389 silc_buffer_pull(conn->outbuf, ret);
392 /* Data sent, close connection */
393 SILC_LOG_DEBUG(("Data sent"));
394 silc_http_server_close_connection(conn);
398 conn->keepalive = FALSE;
399 silc_http_server_close_connection(conn);
404 /* Accepts new connection */
406 static void silc_http_server_new_connection(SilcNetStatus status,
410 SilcHttpServer httpd = context;
411 SilcHttpConnection conn;
412 const char *hostname = NULL, *ip = NULL;
414 /* Get free connection */
415 silc_list_start(httpd->conns);
416 conn = silc_list_get(httpd->conns);
418 /* Add new connection */
419 conn = silc_http_server_alloc_connection();
421 silc_stream_destroy(stream);
424 silc_list_add(httpd->allconns, conn);
426 silc_list_del(httpd->conns, conn);
429 conn->stream = stream;
431 silc_socket_stream_get_info(stream, NULL, &hostname, &ip, NULL);
432 SILC_LOG_INFO(("HTTPD: New connection %s (%s)", hostname, ip));
433 SILC_LOG_DEBUG(("New connection %p", conn));
435 /* Schedule the connection for data I/O */
436 silc_stream_set_notifier(stream, httpd->schedule, silc_http_server_io, conn);
438 /* Add connection timeout check */
439 silc_schedule_task_add_timeout(httpd->schedule,
440 silc_http_server_connection_timeout, conn,
441 SILC_HTTP_SERVER_TIMEOUT, 0);
445 /******************************* Public API *********************************/
447 /* Allocate HTTP server */
449 SilcHttpServer silc_http_server_alloc(const char *ip, SilcUInt16 port,
450 SilcSchedule schedule,
451 SilcHttpServerCallback callback,
454 SilcHttpServer httpd;
455 SilcHttpConnection conn;
458 SILC_LOG_DEBUG(("Start HTTP server at %s:%d", ip, port));
460 if (!ip || !schedule || !callback)
463 httpd = silc_calloc(1, sizeof(*httpd));
467 /* Create server listener */
469 silc_net_tcp_create_listener(&ip, 1, port, TRUE, FALSE, schedule,
470 silc_http_server_new_connection, httpd);
471 if (!httpd->listener) {
472 SILC_LOG_ERROR(("Could not bind HTTP server at %s:%d", ip, port));
473 silc_http_server_free(httpd);
477 httpd->schedule = schedule;
478 httpd->callback = callback;
479 httpd->context = context;
481 silc_list_init(httpd->conns, struct SilcHttpConnectionStruct, next);
482 silc_list_init(httpd->allconns, struct SilcHttpConnectionStruct, next2);
484 /* Allocate connections list */
485 for (i = 0; i < SILC_HTTP_SERVER_CONNS; i++) {
486 conn = silc_http_server_alloc_connection();
489 silc_list_add(httpd->conns, conn);
490 silc_list_add(httpd->allconns, conn);
494 SILC_LOG_DEBUG(("HTTP Server started"));
499 /* Free HTTP server */
501 void silc_http_server_free(SilcHttpServer httpd)
503 SilcHttpConnection conn;
505 silc_list_start(httpd->allconns);
506 while ((conn = silc_list_get(httpd->allconns))) {
507 conn->keepalive = FALSE;
508 if (conn->httpd && conn->stream)
509 silc_http_server_close_connection(conn);
510 silc_buffer_free(conn->inbuf);
511 silc_buffer_free(conn->outbuf);
516 silc_net_close_listener(httpd->listener);
521 /* Send HTTP data to connection */
523 SilcBool silc_http_server_send(SilcHttpServer httpd,
524 SilcHttpConnection conn,
528 unsigned char *headers, tmp[16];
529 SilcUInt32 headers_len;
532 SILC_LOG_DEBUG(("Sending HTTP data"));
534 conn->touched = silc_time();
537 silc_buffer_set(&h, SILC_HTTP_SERVER_HEADER,
538 strlen(SILC_HTTP_SERVER_HEADER));
539 ret = silc_http_server_send_internal(httpd, conn, &h, TRUE);
541 conn->keepalive = FALSE;
542 silc_http_server_close_connection(conn);
546 if (!conn->headers) {
547 conn->headers = silc_mime_alloc();
548 if (!conn->headers) {
549 conn->keepalive = FALSE;
550 silc_http_server_close_connection(conn);
555 silc_mime_add_field(conn->headers, "Last-Modified",
556 silc_time_string(conn->touched));
557 silc_snprintf(tmp, sizeof(tmp), "%d", (int)silc_buffer_len(data));
558 silc_mime_add_field(conn->headers, "Content-Length", tmp);
559 if (conn->keepalive) {
560 silc_mime_add_field(conn->headers, "Connection", "keep-alive");
561 silc_snprintf(tmp, sizeof(tmp), "%d", (int)SILC_HTTP_SERVER_TIMEOUT);
562 silc_mime_add_field(conn->headers, "Keep-alive", tmp);
565 headers = silc_mime_encode(conn->headers, &headers_len);
567 silc_buffer_set(&h, headers, headers_len);
568 if (!silc_http_server_send_internal(httpd, conn, &h, TRUE)) {
569 conn->keepalive = FALSE;
570 silc_http_server_close_connection(conn);
576 /* Write the page data */
577 return silc_http_server_send_internal(httpd, conn, data, FALSE);
580 /* Send error reply */
582 SilcBool silc_http_server_send_error(SilcHttpServer httpd,
583 SilcHttpConnection conn,
585 const char *error_message)
588 SilcBufferStruct data;
590 memset(&data, 0, sizeof(data));
591 silc_buffer_strformat(&data,
592 "HTTP/1.1 ", error, "\r\n\r\n", error_message,
595 /* Send the message */
596 ret = silc_http_server_send_internal(httpd, conn, &data, FALSE);
598 silc_buffer_purge(&data);
600 /* Close connection */
601 conn->keepalive = FALSE;
602 silc_http_server_close_connection(conn);
609 const char *silc_http_server_get_header(SilcHttpServer httpd,
610 SilcHttpConnection conn,
613 if (!conn->curheaders)
615 return silc_mime_get_field(conn->curheaders, field);
620 SilcBool silc_http_server_add_header(SilcHttpServer httpd,
621 SilcHttpConnection conn,
625 SILC_LOG_DEBUG(("Adding header %s:%s", field, value));
627 if (!conn->headers) {
628 conn->headers = silc_mime_alloc();
629 if (!conn->headers) {
630 silc_http_server_close_connection(conn);
635 silc_mime_add_field(conn->headers, field, value);