Lines Matching refs:httpd

24 #include <isc/httpd.h>
96 * Flags on the httpd client.
136 /*% lightweight socket manager for httpd output */
219 static void reset_client(isc_httpd_t *httpd);
234 isc_httpd_t *httpd = *httpdp;
235 isc_httpdmgr_t *httpdmgr = httpd->mgr;
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,
246 httpd->headerlen);
248 isc_mem_put(httpdmgr->mctx, httpd, sizeof(isc_httpd_t));
262 isc_httpdmgr_t *httpd;
270 httpd = isc_mem_get(mctx, sizeof(isc_httpdmgr_t));
271 if (httpd == NULL)
274 result = isc_mutex_init(&httpd->lock);
276 isc_mem_put(mctx, httpd, sizeof(isc_httpdmgr_t));
279 httpd->mctx = NULL;
280 isc_mem_attach(mctx, &httpd->mctx);
281 httpd->sock = NULL;
282 isc_socket_attach(sock, &httpd->sock);
283 httpd->task = NULL;
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);
304 result = isc_socket_accept(sock, task, isc_httpd_accept, httpd);
308 httpd->render_404 = render_404;
309 httpd->render_500 = render_500;
311 *httpdp = httpd;
315 isc_task_detach(&httpd->task);
316 isc_socket_detach(&httpd->sock);
317 isc_mem_detach(&httpd->mctx);
318 (void)isc_mutex_destroy(&httpd->lock);
319 isc_mem_put(mctx, httpd, sizeof(isc_httpdmgr_t));
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;
398 s = strstr(httpd->recvbuf, "\r\n\r\n");
401 s = strstr(httpd->recvbuf, "\n\n");
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;
464 httpd->url = p;
472 httpd->querystring = strchr(httpd->url, '?');
473 if (httpd->querystring != NULL) {
474 *(httpd->querystring) = 0;
475 httpd->querystring++;
493 httpd->protocol = p;
498 httpd->flags |= HTTPD_CLOSE;
501 httpd->flags |= HTTPD_FOUNDHOST;
506 if (strcmp(httpd->protocol, "HTTP/1.1") == 0
507 && ((httpd->flags & HTTPD_FOUNDHOST) == 0))
520 isc_httpd_t *httpd;
551 httpd = isc_mem_get(httpdmgr->mctx, sizeof(isc_httpd_t));
552 if (httpd == NULL) {
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);
565 httpd->flags = 0;
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));
576 httpd->headerlen = HTTP_SENDGROW;
577 isc_buffer_init(&httpd->headerbuffer, httpd->headerdata,
578 httpd->headerlen);
580 ISC_LIST_INIT(httpd->bufflist);
582 isc_buffer_initnull(&httpd->bodybuffer);
583 reset_client(httpd);
585 r.base = (unsigned char *)httpd->recvbuf;
587 result = isc_socket_recv(httpd->sock, &r, 1, task, isc_httpd_recvdone,
588 httpd);
664 isc_httpd_t *httpd = ev->ev_arg;
672 INSIST(ISC_HTTPD_ISRECV(httpd));
676 destroy_client(&httpd);
680 result = process_request(httpd, sev->n);
682 if (httpd->recvlen >= HTTP_RECVLEN - 1) {
683 destroy_client(&httpd);
686 r.base = (unsigned char *)httpd->recvbuf + httpd->recvlen;
687 r.length = HTTP_RECVLEN - httpd->recvlen - 1;
689 (void)isc_socket_recv(httpd->sock, &r, 1, task,
690 isc_httpd_recvdone, httpd);
693 destroy_client(&httpd);
697 ISC_HTTPD_SETSEND(httpd);
703 isc_buffer_initnull(&httpd->bodybuffer);
706 url = ISC_LIST_HEAD(httpd->mgr->urls);
708 if (strcmp(httpd->url, url->url) == 0)
713 result = httpd->mgr->render_404(httpd->url, httpd->querystring,
715 &httpd->retcode,
716 &httpd->retmsg,
717 &httpd->mimetype,
718 &httpd->bodybuffer,
719 &httpd->freecb,
720 &httpd->freecb_arg);
722 result = url->action(httpd->url, httpd->querystring,
724 &httpd->retcode, &httpd->retmsg,
725 &httpd->mimetype, &httpd->bodybuffer,
726 &httpd->freecb, &httpd->freecb_arg);
728 result = httpd->mgr->render_500(httpd->url, httpd->querystring,
729 NULL, &httpd->retcode,
730 &httpd->retmsg,
731 &httpd->mimetype,
732 &httpd->bodybuffer,
733 &httpd->freecb,
734 &httpd->freecb_arg);
738 isc_httpd_response(httpd);
739 isc_httpd_addheader(httpd, "Content-Type", httpd->mimetype);
740 isc_httpd_addheader(httpd, "Date", datebuf);
741 isc_httpd_addheader(httpd, "Expires", datebuf);
742 isc_httpd_addheader(httpd, "Last-Modified", datebuf);
743 isc_httpd_addheader(httpd, "Pragma: no-cache", NULL);
744 isc_httpd_addheader(httpd, "Cache-Control: no-cache", NULL);
745 isc_httpd_addheader(httpd, "Server: libisc", NULL);
746 isc_httpd_addheaderuint(httpd, "Content-Length",
747 isc_buffer_usedlength(&httpd->bodybuffer));
748 isc_httpd_endheaders(httpd); /* done */
750 ISC_LIST_APPEND(httpd->bufflist, &httpd->headerbuffer, link);
756 if (isc_buffer_length(&httpd->bodybuffer) > 0)
757 ISC_LIST_APPEND(httpd->bufflist, &httpd->bodybuffer, link);
760 (void)isc_socket_sendv(httpd->sock, &httpd->bufflist, task,
761 isc_httpd_senddone, httpd);
772 isc_httpd_t *httpd;
784 httpd = ISC_LIST_HEAD(httpdmgr->running);
785 while (httpd != NULL) {
786 isc_socket_cancel(httpd->sock, httpdmgr->task,
788 httpd = ISC_LIST_NEXT(httpd, link);
797 grow_headerspace(isc_httpd_t *httpd)
803 newlen = httpd->headerlen + HTTP_SENDGROW;
807 newspace = isc_mem_get(httpd->mgr->mctx, newlen);
810 isc_buffer_region(&httpd->headerbuffer, &r);
811 isc_buffer_reinit(&httpd->headerbuffer, newspace, newlen);
813 isc_mem_put(httpd->mgr->mctx, r.base, r.length);
819 isc_httpd_response(isc_httpd_t *httpd)
824 needlen = strlen(httpd->protocol) + 1; /* protocol + space */
826 needlen += strlen(httpd->retmsg) + 2; /* return msg + CRLF */
828 while (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
829 result = grow_headerspace(httpd);
834 sprintf(isc_buffer_used(&httpd->headerbuffer), "%s %03d %s\r\n",
835 httpd->protocol, httpd->retcode, httpd->retmsg);
836 isc_buffer_add(&httpd->headerbuffer, needlen);
842 isc_httpd_addheader(isc_httpd_t *httpd, const char *name,
853 while (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
854 result = grow_headerspace(httpd);
860 sprintf(isc_buffer_used(&httpd->headerbuffer),
863 sprintf(isc_buffer_used(&httpd->headerbuffer),
866 isc_buffer_add(&httpd->headerbuffer, needlen);
872 isc_httpd_endheaders(isc_httpd_t *httpd)
876 while (isc_buffer_availablelength(&httpd->headerbuffer) < 2) {
877 result = grow_headerspace(httpd);
882 sprintf(isc_buffer_used(&httpd->headerbuffer), "\r\n");
883 isc_buffer_add(&httpd->headerbuffer, 2);
889 isc_httpd_addheaderuint(isc_httpd_t *httpd, const char *name, int val) {
900 while (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
901 result = grow_headerspace(httpd);
906 sprintf(isc_buffer_used(&httpd->headerbuffer),
909 isc_buffer_add(&httpd->headerbuffer, needlen);
917 isc_httpd_t *httpd = ev->ev_arg;
922 INSIST(ISC_HTTPD_ISSEND(httpd));
930 ISC_LIST_UNLINK(sev->bufferlist, &httpd->headerbuffer, link);
939 if (httpd->freecb != NULL) {
941 if (isc_buffer_length(&httpd->bodybuffer) > 0)
942 b = &httpd->bodybuffer;
943 httpd->freecb(b, httpd->freecb_arg);
946 if (ISC_LINK_LINKED(&httpd->bodybuffer, link)) {
947 ISC_LIST_UNLINK(sev->bufferlist, &httpd->bodybuffer, link);
952 destroy_client(&httpd);
956 if ((httpd->flags & HTTPD_CLOSE) != 0) {
957 destroy_client(&httpd);
961 ISC_HTTPD_SETRECV(httpd);
965 reset_client(httpd);
967 r.base = (unsigned char *)httpd->recvbuf;
970 (void)isc_socket_recv(httpd->sock, &r, 1, task,
971 isc_httpd_recvdone, httpd);
979 reset_client(isc_httpd_t *httpd)
985 INSIST(ISC_HTTPD_ISRECV(httpd));
986 INSIST(!ISC_LINK_LINKED(&httpd->headerbuffer, link));
987 INSIST(!ISC_LINK_LINKED(&httpd->bodybuffer, link));
989 httpd->recvbuf[0] = 0;
990 httpd->recvlen = 0;
991 httpd->method = ISC_HTTPD_METHODUNKNOWN;
992 httpd->url = NULL;
993 httpd->querystring = NULL;
994 httpd->protocol = NULL;
995 httpd->flags = 0;
997 isc_buffer_clear(&httpd->headerbuffer);
998 isc_buffer_invalidate(&httpd->bodybuffer);