2 * Copyright (C) 2006-2008, 2010 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: httpd.c,v 1.16.64.2 2010/02/04 23:47:46 tbox Exp $ */
23 #include <isc/buffer.h>
24 #include <isc/httpd.h>
26 #include <isc/socket.h>
27 #include <isc/string.h>
36 * o Put in better checks to make certain things are passed in correctly.
37 * This includes a magic number for externally-visible structures,
38 * checking for NULL-ness before dereferencing, etc.
39 * o Make the URL processing external functions which will fill-in a buffer
40 * structure we provide, or return an error and we will render a generic
41 * page and close the client.
44 #define MSHUTTINGDOWN(cm) ((cm->flags & ISC_HTTPDMGR_FLAGSHUTTINGDOWN) != 0)
45 #define MSETSHUTTINGDOWN(cm) (cm->flags |= ISC_HTTPDMGR_FLAGSHUTTINGDOWN)
48 #define ENTER(x) do { fprintf(stderr, "ENTER %s\n", (x)); } while (0)
49 #define EXIT(x) do { fprintf(stderr, "EXIT %s\n", (x)); } while (0)
50 #define NOTICE(x) do { fprintf(stderr, "NOTICE %s\n", (x)); } while (0)
52 #define ENTER(x) do { } while(0)
53 #define EXIT(x) do { } while(0)
54 #define NOTICE(x) do { } while(0)
57 #define HTTP_RECVLEN 1024
58 #define HTTP_SENDGROW 1024
59 #define HTTP_SEND_MAXLEN 10240
62 * HTTP urls. These are the URLs we manage, and the function to call to
63 * provide the data for it. We pass in the base url (so the same function
64 * can handle multiple requests), and a structure to fill in to return a
65 * result to the client. We also pass in a pointer to be filled in for
66 * the data cleanup function.
70 isc_httpdaction_t *action;
72 ISC_LINK(isc_httpdurl_t) link;
75 #define HTTPD_CLOSE 0x0001 /* Got a Connection: close header */
76 #define HTTPD_FOUNDHOST 0x0002 /* Got a Host: header */
80 isc_httpdmgr_t *mgr; /*%< our parent */
81 ISC_LINK(isc_httpd_t) link;
86 * Received data state.
88 char recvbuf[HTTP_RECVLEN]; /*%< receive buffer */
89 isc_uint32_t recvlen; /*%< length recv'd */
96 * Flags on the httpd client.
101 * Transmit data state.
103 * This is the data buffer we will transmit.
105 * This free function pointer is filled in by the rendering function
106 * we call. The free function is called after the data is transmitted
109 * The bufflist is the list of buffers we are currently transmitting.
110 * The headerdata is where we render our headers to. If we run out of
111 * space when rendering a header, we will change the size of our
112 * buffer. We will not free it until we are finished, and will
113 * allocate an additional HTTP_SENDGROW bytes per header space grow.
115 * We currently use two buffers total, one for the headers (which
116 * we manage) and another for the client to fill in (which it manages,
117 * it provides the space for it, etc) -- we will pass that buffer
118 * structure back to the caller, who is responsible for managing the
119 * space it may have allocated as backing store for it. This second
120 * buffer is bodybuffer, and we only allocate the buffer itself, not
123 isc_bufferlist_t bufflist;
124 char *headerdata; /*%< send header buf */
125 unsigned int headerlen; /*%< current header buffer size */
126 isc_buffer_t headerbuffer;
128 const char *mimetype;
129 unsigned int retcode;
131 isc_buffer_t bodybuffer;
132 isc_httpdfree_t *freecb;
136 /*% lightweight socket manager for httpd output */
137 struct isc_httpdmgr {
139 isc_socket_t *sock; /*%< listening socket */
140 isc_task_t *task; /*%< owning task */
141 isc_timermgr_t *timermgr;
143 isc_httpdclientok_t *client_ok; /*%< client validator */
144 isc_httpdondestroy_t *ondestroy; /*%< cleanup callback */
145 void *cb_arg; /*%< argument for the above */
148 ISC_LIST(isc_httpd_t) running; /*%< running clients */
152 ISC_LIST(isc_httpdurl_t) urls; /*%< urls we manage */
153 isc_httpdaction_t *render_404;
154 isc_httpdaction_t *render_500;
160 #define ISC_HTTPD_METHODUNKNOWN 0
161 #define ISC_HTTPD_METHODGET 1
162 #define ISC_HTTPD_METHODPOST 2
167 * _IDLE The client is not doing anything at all. This state should
168 * only occur just after creation, and just before being
171 * _RECV The client is waiting for data after issuing a socket recv().
173 * _RECVDONE Data has been received, and is being processed.
175 * _SEND All data for a response has completed, and a reply was
176 * sent via a socket send() call.
178 * _SENDDONE Send is completed.
180 * Badly formatted state table:
182 * IDLE -> RECV when client has a recv() queued.
184 * RECV -> RECVDONE when recvdone event received.
186 * RECVDONE -> SEND if the data for a reply is at hand.
188 * SEND -> RECV when a senddone event was received.
190 * At any time -> RECV on error. If RECV fails, the client will
191 * self-destroy, closing the socket and freeing memory.
193 #define ISC_HTTPD_STATEIDLE 0
194 #define ISC_HTTPD_STATERECV 1
195 #define ISC_HTTPD_STATERECVDONE 2
196 #define ISC_HTTPD_STATESEND 3
197 #define ISC_HTTPD_STATESENDDONE 4
199 #define ISC_HTTPD_ISRECV(c) ((c)->state == ISC_HTTPD_STATERECV)
200 #define ISC_HTTPD_ISRECVDONE(c) ((c)->state == ISC_HTTPD_STATERECVDONE)
201 #define ISC_HTTPD_ISSEND(c) ((c)->state == ISC_HTTPD_STATESEND)
202 #define ISC_HTTPD_ISSENDDONE(c) ((c)->state == ISC_HTTPD_STATESENDDONE)
205 * Overall magic test that means we're not idle.
207 #define ISC_HTTPD_SETRECV(c) ((c)->state = ISC_HTTPD_STATERECV)
208 #define ISC_HTTPD_SETRECVDONE(c) ((c)->state = ISC_HTTPD_STATERECVDONE)
209 #define ISC_HTTPD_SETSEND(c) ((c)->state = ISC_HTTPD_STATESEND)
210 #define ISC_HTTPD_SETSENDDONE(c) ((c)->state = ISC_HTTPD_STATESENDDONE)
212 static void isc_httpd_accept(isc_task_t *, isc_event_t *);
213 static void isc_httpd_recvdone(isc_task_t *, isc_event_t *);
214 static void isc_httpd_senddone(isc_task_t *, isc_event_t *);
215 static void destroy_client(isc_httpd_t **);
216 static isc_result_t process_request(isc_httpd_t *, int);
217 static void httpdmgr_destroy(isc_httpdmgr_t *);
218 static isc_result_t grow_headerspace(isc_httpd_t *);
219 static void reset_client(isc_httpd_t *httpd);
220 static isc_result_t render_404(const char *, const char *,
222 unsigned int *, const char **,
223 const char **, isc_buffer_t *,
224 isc_httpdfree_t **, void **);
225 static isc_result_t render_500(const char *, const char *,
227 unsigned int *, const char **,
228 const char **, isc_buffer_t *,
229 isc_httpdfree_t **, void **);
232 destroy_client(isc_httpd_t **httpdp)
234 isc_httpd_t *httpd = *httpdp;
235 isc_httpdmgr_t *httpdmgr = httpd->mgr;
239 LOCK(&httpdmgr->lock);
241 isc_socket_detach(&httpd->sock);
242 ISC_LIST_UNLINK(httpdmgr->running, httpd, link);
244 if (httpd->headerlen > 0)
245 isc_mem_put(httpdmgr->mctx, httpd->headerdata,
248 isc_mem_put(httpdmgr->mctx, httpd, sizeof(isc_httpd_t));
250 UNLOCK(&httpdmgr->lock);
252 httpdmgr_destroy(httpdmgr);
256 isc_httpdmgr_create(isc_mem_t *mctx, isc_socket_t *sock, isc_task_t *task,
257 isc_httpdclientok_t *client_ok,
258 isc_httpdondestroy_t *ondestroy, void *cb_arg,
259 isc_timermgr_t *tmgr, isc_httpdmgr_t **httpdp)
262 isc_httpdmgr_t *httpd;
264 REQUIRE(mctx != NULL);
265 REQUIRE(sock != NULL);
266 REQUIRE(task != NULL);
267 REQUIRE(tmgr != NULL);
268 REQUIRE(httpdp != NULL && *httpdp == NULL);
270 httpd = isc_mem_get(mctx, sizeof(isc_httpdmgr_t));
272 return (ISC_R_NOMEMORY);
274 result = isc_mutex_init(&httpd->lock);
275 if (result != ISC_R_SUCCESS) {
276 isc_mem_put(mctx, httpd, sizeof(isc_httpdmgr_t));
280 isc_mem_attach(mctx, &httpd->mctx);
282 isc_socket_attach(sock, &httpd->sock);
284 isc_task_attach(task, &httpd->task);
285 httpd->timermgr = tmgr; /* XXXMLG no attach function? */
286 httpd->client_ok = client_ok;
287 httpd->ondestroy = ondestroy;
288 httpd->cb_arg = cb_arg;
290 ISC_LIST_INIT(httpd->running);
291 ISC_LIST_INIT(httpd->urls);
293 /* XXXMLG ignore errors on isc_socket_listen() */
294 result = isc_socket_listen(sock, SOMAXCONN);
295 if (result != ISC_R_SUCCESS) {
296 UNEXPECTED_ERROR(__FILE__, __LINE__,
297 "isc_socket_listen() failed: %s",
298 isc_result_totext(result));
302 (void)isc_socket_filter(sock, "httpready");
304 result = isc_socket_accept(sock, task, isc_httpd_accept, httpd);
305 if (result != ISC_R_SUCCESS)
308 httpd->render_404 = render_404;
309 httpd->render_500 = render_500;
312 return (ISC_R_SUCCESS);
315 isc_task_detach(&httpd->task);
316 isc_socket_detach(&httpd->sock);
317 isc_mem_detach(&httpd->mctx);
318 isc_mutex_destroy(&httpd->lock);
319 isc_mem_put(mctx, httpd, sizeof(isc_httpdmgr_t));
324 httpdmgr_destroy(isc_httpdmgr_t *httpdmgr)
329 ENTER("httpdmgr_destroy");
331 LOCK(&httpdmgr->lock);
333 if (!MSHUTTINGDOWN(httpdmgr)) {
334 NOTICE("httpdmgr_destroy not shutting down yet");
335 UNLOCK(&httpdmgr->lock);
340 * If all clients are not shut down, don't do anything yet.
342 if (!ISC_LIST_EMPTY(httpdmgr->running)) {
343 NOTICE("httpdmgr_destroy clients still active");
344 UNLOCK(&httpdmgr->lock);
348 NOTICE("httpdmgr_destroy detaching socket, task, and timermgr");
350 isc_socket_detach(&httpdmgr->sock);
351 isc_task_detach(&httpdmgr->task);
352 httpdmgr->timermgr = NULL;
355 * Clear out the list of all actions we know about. Just free the
358 url = ISC_LIST_HEAD(httpdmgr->urls);
359 while (url != NULL) {
360 isc_mem_free(httpdmgr->mctx, url->url);
361 ISC_LIST_UNLINK(httpdmgr->urls, url, link);
362 isc_mem_put(httpdmgr->mctx, url, sizeof(isc_httpdurl_t));
363 url = ISC_LIST_HEAD(httpdmgr->urls);
366 UNLOCK(&httpdmgr->lock);
367 isc_mutex_destroy(&httpdmgr->lock);
369 if (httpdmgr->ondestroy != NULL)
370 (httpdmgr->ondestroy)(httpdmgr->cb_arg);
372 mctx = httpdmgr->mctx;
373 isc_mem_putanddetach(&mctx, httpdmgr, sizeof(isc_httpdmgr_t));
375 EXIT("httpdmgr_destroy");
378 #define LENGTHOK(s) (httpd->recvbuf - (s) < (int)httpd->recvlen)
379 #define BUFLENOK(s) (httpd->recvbuf - (s) < HTTP_RECVLEN)
382 process_request(isc_httpd_t *httpd, int length)
390 httpd->recvlen += length;
392 httpd->recvbuf[httpd->recvlen] = 0;
395 * If we don't find a blank line in our buffer, return that we need
398 s = strstr(httpd->recvbuf, "\r\n\r\n");
401 s = strstr(httpd->recvbuf, "\n\n");
405 return (ISC_R_NOTFOUND);
408 * Determine if this is a POST or GET method. Any other values will
409 * cause an error to be returned.
411 if (strncmp(httpd->recvbuf, "GET ", 4) == 0) {
412 httpd->method = ISC_HTTPD_METHODGET;
413 p = httpd->recvbuf + 4;
414 } else if (strncmp(httpd->recvbuf, "POST ", 5) == 0) {
415 httpd->method = ISC_HTTPD_METHODPOST;
416 p = httpd->recvbuf + 5;
418 return (ISC_R_RANGE);
422 * From now on, p is the start of our buffer.
429 while (LENGTHOK(s) && BUFLENOK(s) &&
430 (*s != '\n' && *s != '\r' && *s != '\0' && *s != ' '))
433 return (ISC_R_NOTFOUND);
435 return (ISC_R_NOMEMORY);
439 * Make the URL relative.
441 if ((strncmp(p, "http:/", 6) == 0)
442 || (strncmp(p, "https:/", 7) == 0)) {
444 while (*p != '/' && *p != 0)
447 return (ISC_R_RANGE);
450 while (*p != '/' && *p != 0)
453 return (ISC_R_RANGE);
456 while (*p != '/' && *p != 0)
469 * Now, see if there is a ? mark in the URL. If so, this is
470 * part of the query string, and we will split it from the URL.
472 httpd->querystring = strchr(httpd->url, '?');
473 if (httpd->querystring != NULL) {
474 *(httpd->querystring) = 0;
475 httpd->querystring++;
479 * Extract the HTTP/1.X protocol. We will bounce on anything but
482 while (LENGTHOK(s) && BUFLENOK(s) &&
483 (*s != '\n' && *s != '\r' && *s != '\0'))
486 return (ISC_R_NOTFOUND);
488 return (ISC_R_NOMEMORY);
490 if ((strncmp(p, "HTTP/1.0", 8) != 0)
491 && (strncmp(p, "HTTP/1.1", 8) != 0))
492 return (ISC_R_RANGE);
497 if (strstr(s, "Connection: close") != NULL)
498 httpd->flags |= HTTPD_CLOSE;
500 if (strstr(s, "Host: ") != NULL)
501 httpd->flags |= HTTPD_FOUNDHOST;
504 * Standards compliance hooks here.
506 if (strcmp(httpd->protocol, "HTTP/1.1") == 0
507 && ((httpd->flags & HTTPD_FOUNDHOST) == 0))
508 return (ISC_R_RANGE);
512 return (ISC_R_SUCCESS);
516 isc_httpd_accept(isc_task_t *task, isc_event_t *ev)
519 isc_httpdmgr_t *httpdmgr = ev->ev_arg;
522 isc_socket_newconnev_t *nev = (isc_socket_newconnev_t *)ev;
523 isc_sockaddr_t peeraddr;
527 LOCK(&httpdmgr->lock);
528 if (MSHUTTINGDOWN(httpdmgr)) {
529 NOTICE("accept shutting down, goto out");
533 if (nev->result == ISC_R_CANCELED) {
534 NOTICE("accept canceled, goto out");
538 if (nev->result != ISC_R_SUCCESS) {
539 /* XXXMLG log failure */
540 NOTICE("accept returned failure, goto requeue");
544 (void)isc_socket_getpeername(nev->newsocket, &peeraddr);
545 if (httpdmgr->client_ok != NULL &&
546 !(httpdmgr->client_ok)(&peeraddr, httpdmgr->cb_arg)) {
547 isc_socket_detach(&nev->newsocket);
551 httpd = isc_mem_get(httpdmgr->mctx, sizeof(isc_httpd_t));
553 /* XXXMLG log failure */
554 NOTICE("accept failed to allocate memory, goto requeue");
555 isc_socket_detach(&nev->newsocket);
559 httpd->mgr = httpdmgr;
560 ISC_LINK_INIT(httpd, link);
561 ISC_LIST_APPEND(httpdmgr->running, httpd, link);
562 ISC_HTTPD_SETRECV(httpd);
563 httpd->sock = nev->newsocket;
564 isc_socket_setname(httpd->sock, "httpd", NULL);
568 * Initialize the buffer for our headers.
570 httpd->headerdata = isc_mem_get(httpdmgr->mctx, HTTP_SENDGROW);
571 if (httpd->headerdata == NULL) {
572 isc_mem_put(httpdmgr->mctx, httpd, sizeof(isc_httpd_t));
573 isc_socket_detach(&nev->newsocket);
576 httpd->headerlen = HTTP_SENDGROW;
577 isc_buffer_init(&httpd->headerbuffer, httpd->headerdata,
580 ISC_LIST_INIT(httpd->bufflist);
582 isc_buffer_initnull(&httpd->bodybuffer);
585 r.base = (unsigned char *)httpd->recvbuf;
586 r.length = HTTP_RECVLEN - 1;
587 result = isc_socket_recv(httpd->sock, &r, 1, task, isc_httpd_recvdone,
589 NOTICE("accept queued recv on socket");
592 result = isc_socket_accept(httpdmgr->sock, task, isc_httpd_accept,
594 if (result != ISC_R_SUCCESS) {
595 /* XXXMLG what to do? Log failure... */
596 NOTICE("accept could not reaccept due to failure");
600 UNLOCK(&httpdmgr->lock);
602 httpdmgr_destroy(httpdmgr);
610 render_404(const char *url, const char *querystring,
612 unsigned int *retcode, const char **retmsg,
613 const char **mimetype, isc_buffer_t *b,
614 isc_httpdfree_t **freecb, void **freecb_args)
616 static char msg[] = "No such URL.";
623 *retmsg = "No such URL";
624 *mimetype = "text/plain";
625 isc_buffer_reinit(b, msg, strlen(msg));
626 isc_buffer_add(b, strlen(msg));
630 return (ISC_R_SUCCESS);
634 render_500(const char *url, const char *querystring,
636 unsigned int *retcode, const char **retmsg,
637 const char **mimetype, isc_buffer_t *b,
638 isc_httpdfree_t **freecb, void **freecb_args)
640 static char msg[] = "Internal server failure.";
647 *retmsg = "Internal server failure";
648 *mimetype = "text/plain";
649 isc_buffer_reinit(b, msg, strlen(msg));
650 isc_buffer_add(b, strlen(msg));
654 return (ISC_R_SUCCESS);
658 isc_httpd_recvdone(isc_task_t *task, isc_event_t *ev)
662 isc_httpd_t *httpd = ev->ev_arg;
663 isc_socketevent_t *sev = (isc_socketevent_t *)ev;
666 char datebuf[32]; /* Only need 30, but safety first */
670 INSIST(ISC_HTTPD_ISRECV(httpd));
672 if (sev->result != ISC_R_SUCCESS) {
673 NOTICE("recv destroying client");
674 destroy_client(&httpd);
678 result = process_request(httpd, sev->n);
679 if (result == ISC_R_NOTFOUND) {
680 if (httpd->recvlen >= HTTP_RECVLEN - 1) {
681 destroy_client(&httpd);
684 r.base = (unsigned char *)httpd->recvbuf + httpd->recvlen;
685 r.length = HTTP_RECVLEN - httpd->recvlen - 1;
686 result = isc_socket_recv(httpd->sock, &r, 1, task,
687 isc_httpd_recvdone, httpd);
689 } else if (result != ISC_R_SUCCESS) {
690 destroy_client(&httpd);
694 ISC_HTTPD_SETSEND(httpd);
697 * XXXMLG Call function here. Provide an add-header function
698 * which will append the common headers to a response we generate.
700 isc_buffer_initnull(&httpd->bodybuffer);
702 isc_time_formathttptimestamp(&now, datebuf, sizeof(datebuf));
703 url = ISC_LIST_HEAD(httpd->mgr->urls);
704 while (url != NULL) {
705 if (strcmp(httpd->url, url->url) == 0)
707 url = ISC_LIST_NEXT(url, link);
710 result = httpd->mgr->render_404(httpd->url, httpd->querystring,
719 result = url->action(httpd->url, httpd->querystring,
721 &httpd->retcode, &httpd->retmsg,
722 &httpd->mimetype, &httpd->bodybuffer,
723 &httpd->freecb, &httpd->freecb_arg);
724 if (result != ISC_R_SUCCESS) {
725 result = httpd->mgr->render_500(httpd->url, httpd->querystring,
735 isc_httpd_response(httpd);
736 isc_httpd_addheader(httpd, "Content-Type", httpd->mimetype);
737 isc_httpd_addheader(httpd, "Date", datebuf);
738 isc_httpd_addheader(httpd, "Expires", datebuf);
739 isc_httpd_addheader(httpd, "Last-Modified", datebuf);
740 isc_httpd_addheader(httpd, "Pragma: no-cache", NULL);
741 isc_httpd_addheader(httpd, "Cache-Control: no-cache", NULL);
742 isc_httpd_addheader(httpd, "Server: libisc", NULL);
743 isc_httpd_addheaderuint(httpd, "Content-Length",
744 isc_buffer_usedlength(&httpd->bodybuffer));
745 isc_httpd_endheaders(httpd); /* done */
747 ISC_LIST_APPEND(httpd->bufflist, &httpd->headerbuffer, link);
749 * Link the data buffer into our send queue, should we have any data
750 * rendered into it. If no data is present, we won't do anything
753 if (isc_buffer_length(&httpd->bodybuffer) > 0)
754 ISC_LIST_APPEND(httpd->bufflist, &httpd->bodybuffer, link);
756 result = isc_socket_sendv(httpd->sock, &httpd->bufflist, task,
757 isc_httpd_senddone, httpd);
765 isc_httpdmgr_shutdown(isc_httpdmgr_t **httpdmgrp)
767 isc_httpdmgr_t *httpdmgr;
769 httpdmgr = *httpdmgrp;
772 ENTER("isc_httpdmgr_shutdown");
774 LOCK(&httpdmgr->lock);
776 MSETSHUTTINGDOWN(httpdmgr);
778 isc_socket_cancel(httpdmgr->sock, httpdmgr->task, ISC_SOCKCANCEL_ALL);
780 httpd = ISC_LIST_HEAD(httpdmgr->running);
781 while (httpd != NULL) {
782 isc_socket_cancel(httpd->sock, httpdmgr->task,
784 httpd = ISC_LIST_NEXT(httpd, link);
787 UNLOCK(&httpdmgr->lock);
789 EXIT("isc_httpdmgr_shutdown");
793 grow_headerspace(isc_httpd_t *httpd)
799 newlen = httpd->headerlen + HTTP_SENDGROW;
800 if (newlen > HTTP_SEND_MAXLEN)
801 return (ISC_R_NOSPACE);
803 newspace = isc_mem_get(httpd->mgr->mctx, newlen);
804 if (newspace == NULL)
805 return (ISC_R_NOMEMORY);
806 isc_buffer_region(&httpd->headerbuffer, &r);
807 isc_buffer_reinit(&httpd->headerbuffer, newspace, newlen);
809 isc_mem_put(httpd->mgr->mctx, r.base, r.length);
811 return (ISC_R_SUCCESS);
815 isc_httpd_response(isc_httpd_t *httpd)
818 unsigned int needlen;
820 needlen = strlen(httpd->protocol) + 1; /* protocol + space */
821 needlen += 3 + 1; /* room for response code, always 3 bytes */
822 needlen += strlen(httpd->retmsg) + 2; /* return msg + CRLF */
824 if (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
825 result = grow_headerspace(httpd);
826 if (result != ISC_R_SUCCESS)
830 sprintf(isc_buffer_used(&httpd->headerbuffer), "%s %03d %s\r\n",
831 httpd->protocol, httpd->retcode, httpd->retmsg);
832 isc_buffer_add(&httpd->headerbuffer, needlen);
834 return (ISC_R_SUCCESS);
838 isc_httpd_addheader(isc_httpd_t *httpd, const char *name,
842 unsigned int needlen;
844 needlen = strlen(name); /* name itself */
846 needlen += 2 + strlen(val); /* :<space> and val */
847 needlen += 2; /* CRLF */
849 if (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
850 result = grow_headerspace(httpd);
851 if (result != ISC_R_SUCCESS)
856 sprintf(isc_buffer_used(&httpd->headerbuffer),
857 "%s: %s\r\n", name, val);
859 sprintf(isc_buffer_used(&httpd->headerbuffer),
862 isc_buffer_add(&httpd->headerbuffer, needlen);
864 return (ISC_R_SUCCESS);
868 isc_httpd_endheaders(isc_httpd_t *httpd)
872 if (isc_buffer_availablelength(&httpd->headerbuffer) < 2) {
873 result = grow_headerspace(httpd);
874 if (result != ISC_R_SUCCESS)
878 sprintf(isc_buffer_used(&httpd->headerbuffer), "\r\n");
879 isc_buffer_add(&httpd->headerbuffer, 2);
881 return (ISC_R_SUCCESS);
885 isc_httpd_addheaderuint(isc_httpd_t *httpd, const char *name, int val) {
887 unsigned int needlen;
888 char buf[sizeof "18446744073709551616"];
890 sprintf(buf, "%d", val);
892 needlen = strlen(name); /* name itself */
893 needlen += 2 + strlen(buf); /* :<space> and val */
894 needlen += 2; /* CRLF */
896 if (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
897 result = grow_headerspace(httpd);
898 if (result != ISC_R_SUCCESS)
902 sprintf(isc_buffer_used(&httpd->headerbuffer),
903 "%s: %s\r\n", name, buf);
905 isc_buffer_add(&httpd->headerbuffer, needlen);
907 return (ISC_R_SUCCESS);
911 isc_httpd_senddone(isc_task_t *task, isc_event_t *ev)
913 isc_httpd_t *httpd = ev->ev_arg;
916 isc_socketevent_t *sev = (isc_socketevent_t *)ev;
919 INSIST(ISC_HTTPD_ISSEND(httpd));
922 * First, unlink our header buffer from the socket's bufflist. This
923 * is sort of an evil hack, since we know our buffer will be there,
924 * and we know it's address, so we can just remove it directly.
926 NOTICE("senddone unlinked header");
927 ISC_LIST_UNLINK(sev->bufferlist, &httpd->headerbuffer, link);
930 * We will always want to clean up our receive buffer, even if we
931 * got an error on send or we are shutting down.
933 * We will pass in the buffer only if there is data in it. If
934 * there is no data, we will pass in a NULL.
936 if (httpd->freecb != NULL) {
937 isc_buffer_t *b = NULL;
938 if (isc_buffer_length(&httpd->bodybuffer) > 0)
939 b = &httpd->bodybuffer;
940 httpd->freecb(b, httpd->freecb_arg);
941 NOTICE("senddone free callback performed");
943 if (ISC_LINK_LINKED(&httpd->bodybuffer, link)) {
944 ISC_LIST_UNLINK(sev->bufferlist, &httpd->bodybuffer, link);
945 NOTICE("senddone body buffer unlinked");
948 if (sev->result != ISC_R_SUCCESS) {
949 destroy_client(&httpd);
953 if ((httpd->flags & HTTPD_CLOSE) != 0) {
954 destroy_client(&httpd);
958 ISC_HTTPD_SETRECV(httpd);
960 NOTICE("senddone restarting recv on socket");
964 r.base = (unsigned char *)httpd->recvbuf;
965 r.length = HTTP_RECVLEN - 1;
966 result = isc_socket_recv(httpd->sock, &r, 1, task, isc_httpd_recvdone,
975 reset_client(isc_httpd_t *httpd)
978 * Catch errors here. We MUST be in RECV mode, and we MUST NOT have
979 * any outstanding buffers. If we have buffers, we have a leak.
981 INSIST(ISC_HTTPD_ISRECV(httpd));
982 INSIST(!ISC_LINK_LINKED(&httpd->headerbuffer, link));
983 INSIST(!ISC_LINK_LINKED(&httpd->bodybuffer, link));
985 httpd->recvbuf[0] = 0;
987 httpd->method = ISC_HTTPD_METHODUNKNOWN;
989 httpd->querystring = NULL;
990 httpd->protocol = NULL;
993 isc_buffer_clear(&httpd->headerbuffer);
994 isc_buffer_invalidate(&httpd->bodybuffer);
998 isc_httpdmgr_addurl(isc_httpdmgr_t *httpdmgr, const char *url,
999 isc_httpdaction_t *func, void *arg)
1001 isc_httpdurl_t *item;
1004 httpdmgr->render_404 = func;
1005 return (ISC_R_SUCCESS);
1008 item = isc_mem_get(httpdmgr->mctx, sizeof(isc_httpdurl_t));
1010 return (ISC_R_NOMEMORY);
1012 item->url = isc_mem_strdup(httpdmgr->mctx, url);
1013 if (item->url == NULL) {
1014 isc_mem_put(httpdmgr->mctx, item, sizeof(isc_httpdurl_t));
1015 return (ISC_R_NOMEMORY);
1018 item->action = func;
1019 item->action_arg = arg;
1020 ISC_LINK_INIT(item, link);
1021 ISC_LIST_APPEND(httpdmgr->urls, item, link);
1023 return (ISC_R_SUCCESS);