/*
- * context_loop.c
+ * $Id$
*
* This is the other half of the webserver. It handles the task of hooking
- * up HTTP requests with the session they belong to, using HTTP cookies to
+ * up HTTP requests with the sessions they belong to, using HTTP cookies to
* keep track of things. If the HTTP request doesn't belong to any currently
- * active session, a new session is spawned.
+ * active session, a new session is started.
*
- * $Id$
*/
-#include <ctype.h>
-#include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <stdio.h>
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#ifdef HAVE_LIMITS_H
-#include <limits.h>
-#endif
-#include <netinet/in.h>
-#include <netdb.h>
-#include <string.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
#include "webcit.h"
#include "webserver.h"
-
-/*
- * We keep one of these around for each active session
- */
-struct wc_session {
- struct wc_session *next; /* Next session in list */
- int session_id; /* Session ID */
- int inpipe[2]; /* Data from webserver to session */
- int outpipe[2]; /* Data from session to webserver */
- pthread_mutex_t critter; /* Critical section uses pipes */
- };
-
-struct wc_session *SessionList = NULL;
+#include "modules_init.h"
/* Only one thread may manipulate SessionList at a time... */
-pthread_mutex_t MasterCritter;
+pthread_mutex_t SessionListMutex;
-int GenerateSessionID(void) {
- return getpid();
- }
+wcsession *SessionList = NULL; /**< our sessions ????*/
+pthread_key_t MyConKey; /**< TSD key for MySession() */
+HashList *HttpReqTypes = NULL;
+HashList *HttpHeaderHandler = NULL;
+extern HashList *HandlerHash;
-void gets0(int fd, char buf[]) {
+void DestroyHttpHeaderHandler(void *V)
+{
+ OneHttpHeader *pHdr;
+ pHdr = (OneHttpHeader*) V;
+ FreeStrBuf(&pHdr->Val);
+ free(pHdr);
+}
- buf[0] = 0;
- do {
- buf[strlen(buf)+1] = 0;
- read(fd, &buf[strlen(buf)], 1);
- } while (buf[strlen(buf)-1] >= 32);
- buf[strlen(buf)-1] = 0;
+void shutdown_sessions(void)
+{
+ wcsession *sptr;
+
+ for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
+ sptr->killthis = 1;
}
+}
-/*
- * Collapse multiple cookies on one line
- */
-void req_gets(int sock, char *buf, char *hold) {
- int a;
+void do_housekeeping(void)
+{
+ wcsession *sptr, *ss;
+ wcsession *sessions_to_kill = NULL;
+ int num_sessions = 0;
+ static int num_threads = MIN_WORKER_THREADS;
- if (strlen(hold)==0) {
- client_gets(sock, buf);
- }
- else {
- strcpy(buf, hold);
+ /**
+ * Lock the session list, moving any candidates for euthanasia into
+ * a separate list.
+ */
+ pthread_mutex_lock(&SessionListMutex);
+ num_sessions = 0;
+ for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
+ ++num_sessions;
+
+ /** Kill idle sessions */
+ if ((time(NULL) - (sptr->lastreq)) >
+ (time_t) WEBCIT_TIMEOUT) {
+ sptr->killthis = 1;
}
- strcpy(hold, "");
-
- if (!strncasecmp(buf, "Cookie: ", 8)) {
- for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
- sprintf(hold, "Cookie: %s", &buf[a+1]);
- buf[a]=0;
- while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
- return;
+
+ /** Remove sessions flagged for kill */
+ if (sptr->killthis) {
+
+ /** remove session from linked list */
+ if (sptr == SessionList) {
+ SessionList = SessionList->next;
+ }
+ else for (ss=SessionList;ss!=NULL;ss=ss->next) {
+ if (ss->next == sptr) {
+ ss->next = ss->next->next;
+ }
}
+
+ sptr->next = sessions_to_kill;
+ sessions_to_kill = sptr;
}
}
+ pthread_mutex_unlock(&SessionListMutex);
+
+ /**
+ * Now free up and destroy the culled sessions.
+ */
+ while (sessions_to_kill != NULL) {
+ lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
+ pthread_mutex_lock(&sessions_to_kill->SessionMutex);
+ pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
+ sptr = sessions_to_kill->next;
+
+ session_destroy_modules(&sessions_to_kill);
+ sessions_to_kill = sptr;
+ --num_sessions;
+ }
+
+ /**
+ * If there are more sessions than threads, then we should spawn
+ * more threads ... up to a predefined maximum.
+ */
+ while ( (num_sessions > num_threads)
+ && (num_threads <= MAX_WORKER_THREADS) ) {
+ spawn_another_worker_thread();
+ ++num_threads;
+ lprintf(3, "There are %d sessions and %d threads active.\n",
+ num_sessions, num_threads);
+ }
+}
+
/*
- * Grab a lock on the session, so other threads don't try to access
- * the pipes at the same time.
+ * Wake up occasionally and clean house
*/
-static void lock_session(struct wc_session *session) {
- printf("Locking session %d...\n", session->session_id);
- pthread_mutex_lock(&session->critter);
- printf(" ...got lock\n");
+void housekeeping_loop(void)
+{
+ while (1) {
+ sleeeeeeeeeep(HOUSEKEEPING);
+ do_housekeeping();
}
+}
+
/*
- * Let go of the lock.
+ * Create a Session id
+ * Generate a unique WebCit session ID (which is not the same thing as the
+ * Citadel session ID).
*/
-static void unlock_session(struct wc_session *session) {
- printf("Unlocking.\n");
- pthread_mutex_unlock(&session->critter);
+int GenerateSessionID(void)
+{
+ static int seq = (-1);
+
+ if (seq < 0) {
+ seq = (int) time(NULL);
}
+
+ return ++seq;
+}
-extern const char *defaulthost;
-extern const char *defaultport;
+
+
+
+
+/**
+ * \brief Detects a 'mobile' user agent
+ */
+int is_mobile_ua(char *user_agent) {
+ if (strstr(user_agent,"iPhone OS") != NULL) {
+ return 1;
+ } else if (strstr(user_agent,"Windows CE") != NULL) {
+ return 1;
+ } else if (strstr(user_agent,"SymbianOS") != NULL) {
+ return 1;
+ } else if (strstr(user_agent, "Opera Mobi") != NULL) {
+ return 1;
+ } else if (strstr(user_agent, "Firefox/2.0.0 Opera 9.51 Beta") != NULL) {
+ /* For some reason a new install of Opera 9.51beta decided to spoof. */
+ return 1;
+ }
+ return 0;
+}
+
+/* If it's a "force 404" situation then display the error and bail. */
+void do_404(void)
+{
+ hprintf("HTTP/1.1 404 Not found\r\n");
+ hprintf("Content-Type: text/plain\r\n");
+ wprintf("Not found\r\n");
+ end_burst();
+}
+
+int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
+{
+ const char *Args;
+ void *vLine, *vHandler;
+ const char *Pos = NULL;
+
+
+ Hdr->HR.ReqLine = Line;
+ /* The requesttype... GET, POST... */
+ StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
+ if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
+ (vLine != NULL))
+ {
+ Hdr->HR.eReqType = *(long*)vLine;
+ }
+ else {
+ Hdr->HR.eReqType = eGET;
+ return 1;
+ }
+ StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
+
+ /* the HTTP Version... */
+ StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
+ StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
+
+ if (StrLength(Buf) == 0) {
+ Hdr->HR.eReqType = eGET;
+ return 1;
+ }
+
+ Hdr->this_page = NewStrBufDup(Hdr->HR.ReqLine);
+ /* chop Filename / query arguments */
+ Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
+ if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
+ Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
+ if (Args != NULL) {
+ Args ++; /* skip the ? */
+ Hdr->PlainArgs = NewStrBufPlain(
+ Args,
+ StrLength(Hdr->HR.ReqLine) -
+ (Args - ChrPtr(Hdr->HR.ReqLine)));
+ StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
+ } /* don't parse them yet, maybe we don't even care... */
+
+ /* now lookup what we are going to do with this... */
+ /* skip first slash */
+ StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
+ do {
+ StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
+
+ GetHash(HandlerHash, SKEY(Buf), &vHandler),
+ Hdr->HR.Handler = (WebcitHandler*) vHandler;
+ if (Hdr->HR.Handler == NULL)
+ break;
+ /*
+ * If the request is prefixed by "/webcit" then chop that off. This
+ * allows a front end web server to forward all /webcit requests to us
+ * while still using the same web server port for other things.
+ */
+ if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
+ continue;
+ break;
+ } while (1);
+ /* remove the handlername from the URL */
+ if (Pos != NULL) {
+ StrBufCutLeft(Hdr->HR.ReqLine,
+ Pos - ChrPtr(Hdr->HR.ReqLine));
+ }
+
+ if (Hdr->HR.Handler != NULL) {
+ if ((Hdr->HR.Handler->Flags & BOGUS) != 0)
+ return 1;
+ Hdr->HR.DontNeedAuth = (Hdr->HR.Handler->Flags & ISSTATIC) != 0;
+ }
+
+ Hdr->HTTPHeaders = NewHash(1, NULL);
+ return 0;
+}
+
+int AnalyseHeaders(ParsedHttpHdrs *Hdr)
+{
+ OneHttpHeader *pHdr;
+ void *vHdr;
+ long HKLen;
+ const char *HashKey;
+ HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
+
+ while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) &&
+ (vHdr != NULL)) {
+ pHdr = (OneHttpHeader *)vHdr;
+ if (pHdr->HaveEvaluator)
+ pHdr->H(pHdr->Val, Hdr);
+
+ }
+ DeleteHashPos(&at);
+ return 0;
+}
+
+/*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
/*
- * This loop gets called once for every HTTP connection made to WebCit.
+ * Read in the request
*/
-void *context_loop(int sock) {
- char (*req)[256];
- char buf[256], hold[256];
- int num_lines = 0;
- int a;
- int f;
- int desired_session = 0;
- char str_session[256];
- struct wc_session *sptr;
- struct wc_session *TheSession;
- int ContentLength;
- int CloseSession = 0;
-
- if ((req = malloc((long)sizeof(char[256][256]))) == NULL) {
- sprintf(buf, "Can't malloc buffers; dropping connection.\n");
- fprintf(stderr, "%s", buf);
- write(sock, buf, strlen(buf));
- close (sock);
- pthread_exit(NULL);
+int ReadHTTPRequset (ParsedHttpHdrs *Hdr)
+{
+ const char *pch, *pchs, *pche;
+ OneHttpHeader *pHdr;
+ StrBuf *Line, *LastLine, *HeaderName;
+ int nLine = 0;
+ void *vF;
+ int isbogus = 0;
+
+ HeaderName = NewStrBuf();
+ Hdr->ReadBuf = NewStrBuf();
+ LastLine = NULL;
+ do {
+ nLine ++;
+ Line = NewStrBuf();
+
+ if (ClientGetLine(Hdr, Line) < 0) return 1;
+
+ if (StrLength(Line) == 0) {
+ FreeStrBuf(&Line);
+ continue;
+ }
+ if (nLine == 1) {
+ pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
+ memset(pHdr, 0, sizeof(OneHttpHeader));
+ pHdr->Val = Line;
+ Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
+ lprintf(9, "%s\n", ChrPtr(Line));
+ isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
+ if (isbogus) break;
+ continue;
}
- printf("Reading request from socket %d\n", sock);
+ /* Do we need to Unfold? */
+ if ((LastLine != NULL) &&
+ (isspace(*ChrPtr(Line)))) {
+ pch = pchs = ChrPtr(Line);
+ pche = pchs + StrLength(Line);
+ while (isspace(*pch) && (pch < pche))
+ pch ++;
+ StrBufCutLeft(Line, pch - pchs);
+ StrBufAppendBuf(LastLine, Line, 0);
+
+ FreeStrBuf(&Line);
+ continue;
+ }
+
+ StrBufSanitizeAscii(Line, 'ยง');
+ StrBufExtract_token(HeaderName, Line, 0, ':');
+
+ pchs = ChrPtr(Line);
+ pch = pchs + StrLength(HeaderName) + 1;
+ pche = pchs + StrLength(Line);
+ while (isspace(*pch) && (pch < pche))
+ pch ++;
+ StrBufCutLeft(Line, pch - pchs);
+
+ StrBufUpCase(HeaderName);
+
+ pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
+ memset(pHdr, 0, sizeof(OneHttpHeader));
+ pHdr->Val = Line;
+
+ if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
+ (vF != NULL))
+ {
+ OneHttpHeader *FHdr = (OneHttpHeader*) vF;
+ pHdr->H = FHdr->H;
+ pHdr->HaveEvaluator = 1;
+ }
+ Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
+ LastLine = Line;
+ } while (Line != NULL);
+
+ FreeStrBuf(&HeaderName);
+
+ return isbogus;
+}
+
+
+
+/*
+ * handle one request
+ *
+ * This loop gets called once for every HTTP connection made to WebCit. At
+ * this entry point we have an HTTP socket with a browser allegedly on the
+ * other end, but we have not yet bound to a WebCit session.
+ *
+ * The job of this function is to locate the correct session and bind to it,
+ * or create a session if necessary and bind to it, then run the WebCit
+ * transaction loop. Afterwards, we unbind from the session. When this
+ * function returns, the worker thread is then free to handle another
+ * transaction.
+ */
+void context_loop(ParsedHttpHdrs *Hdr)
+{
+ int isbogus = 0;
+ wcsession *TheSession, *sptr;
+ struct timeval tx_start;
+ struct timeval tx_finish;
+
+ gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
/*
* Find out what it is that the web browser is asking for
*/
- ContentLength = 0;
- do {
- req_gets(sock, buf, hold);
- if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
- desired_session = atoi(&buf[19]);
- }
- if (!strncasecmp(buf, "Content-length: ", 16)) {
- ContentLength = atoi(&buf[16]);
- }
- strcpy(&req[num_lines++][0], buf);
- } while(strlen(buf)>0);
+ isbogus = ReadHTTPRequset(Hdr);
- /*
- * See if there's an existing session open with the desired ID
+ if (!isbogus)
+ isbogus = AnalyseHeaders(Hdr);
+
+ if ((isbogus) ||
+ ((Hdr->HR.Handler != NULL) &&
+ ((Hdr->HR.Handler->Flags & BOGUS) != 0)))
+ {
+ wcsession *Bogus;
+ Bogus = (wcsession *)
+ malloc(sizeof(wcsession));
+ memset(Bogus, 0, sizeof(wcsession));
+ pthread_setspecific(MyConKey, (void *)Bogus);
+ Bogus->Hdr = Hdr;
+ session_new_modules(Bogus);
+ do_404();
+ session_detach_modules(Bogus);
+ http_destroy_modules(Hdr);
+ session_destroy_modules(&Bogus);
+ return;
+ }
+
+ if ((Hdr->HR.Handler != NULL) &&
+ ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
+ {
+ wcsession *Static;
+
+ Static = (wcsession *)
+ malloc(sizeof(wcsession));
+ memset(Static, 0, sizeof(wcsession));
+ pthread_setspecific(MyConKey, (void *)Static);
+ Static->Hdr = Hdr;
+ Static->serv_sock = (-1);
+ Static->chat_sock = (-1);
+ Static->is_mobile = -1;
+ session_new_modules(Static);
+
+ Hdr->HR.Handler->F();
+
+ /* How long did this transaction take? */
+ gettimeofday(&tx_finish, NULL);
+
+ lprintf(9, "SL: Transaction [%s] completed in %ld.%06ld seconds.\n",
+ ChrPtr(Hdr->this_page),
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000
+ );
+
+ session_detach_modules(Static);
+ http_destroy_modules(Hdr);
+ session_destroy_modules(&Static);
+ return;
+ }
+
+ if (Hdr->HR.got_auth == AUTH_BASIC)
+ CheckAuthBasic(Hdr);
+
+/*
+TODO HKEY("/static/nocookies.html?force_close_session=yes"));
+*/
+
+/* dbg_PrintHash(HTTPHeaders, nix, NULL); */
+
+
+ /* Begin parsing the request. * /
+#ifdef TECH_PREVIEW
+ if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
+ (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
+ (strncmp(ChrPtr(ReqLine), "/tiny_mce/", 10) != 0) &&
+ (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0) &&
+ (strstr(ChrPtr(ReqLine), "wholist_section") == NULL)) {
+#endif
+ lprintf(5, "HTTP: %s %s\n", ReqStrs[Hdr.eReqType], ChrPtr(ReqLine));
+#ifdef TECH_PREVIEW
+ }
+#endif
+
+*/
+
+ /**
+ * See if there's an existing session open with the desired ID or user/pass
*/
TheSession = NULL;
- if (desired_session != 0) {
- pthread_mutex_lock(&MasterCritter);
- for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
- if (sptr->session_id == desired_session) {
- TheSession = sptr;
- lock_session(TheSession);
- }
+
+ if (TheSession == NULL) {
+ pthread_mutex_lock(&SessionListMutex);
+ for (sptr = SessionList;
+ ((sptr != NULL) && (TheSession == NULL));
+ sptr = sptr->next) {
+
+ /** If HTTP-AUTH, look for a session with matching credentials */
+ switch (Hdr->HR.got_auth)
+ {
+ case AUTH_BASIC:
+ if ( (Hdr->HR.SessionKey != sptr->SessionKey))
+ continue;
+ GetAuthBasic(Hdr);
+ if ((!strcasecmp(ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username))) &&
+ (!strcasecmp(ChrPtr(Hdr->c_password), ChrPtr(sptr->wc_password))) )
+ TheSession = sptr;
+ break;
+ case AUTH_COOKIE:
+ /** If cookie-session, look for a session with matching session ID */
+ if ( (Hdr->HR.desired_session != 0) &&
+ (sptr->wc_session == Hdr->HR.desired_session))
+ TheSession = sptr;
+ break;
+ case NO_AUTH:
+ break;
}
- pthread_mutex_unlock(&MasterCritter);
}
+ pthread_mutex_unlock(&SessionListMutex);
+ }
- /*
+ /**
* Create a new session if we have to
*/
if (TheSession == NULL) {
- printf("Creating a new session\n");
- pthread_mutex_lock(&MasterCritter);
- TheSession = (struct wc_session *)
- malloc(sizeof(struct wc_session));
- TheSession->session_id = GenerateSessionID();
- pipe(TheSession->inpipe);
- pipe(TheSession->outpipe);
- pthread_mutex_init(&TheSession->critter, NULL);
- lock_session(TheSession);
+ lprintf(3, "Creating a new session\n");
+ TheSession = (wcsession *)
+ malloc(sizeof(wcsession));
+ memset(TheSession, 0, sizeof(wcsession));
+ TheSession->Hdr = Hdr;
+ TheSession->SessionKey = Hdr->HR.SessionKey;
+ TheSession->serv_sock = (-1);
+ TheSession->chat_sock = (-1);
+
+ /* If we're recreating a session that expired, it's best to give it the same
+ * session number that it had before. The client browser ought to pick up
+ * the new session number and start using it, but in some rare situations it
+ * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
+ * to get created.
+ */
+ if (Hdr->HR.desired_session == 0) {
+ TheSession->wc_session = GenerateSessionID();
+ }
+ else {
+ TheSession->wc_session = Hdr->HR.desired_session;
+ }
+
+ pthread_setspecific(MyConKey, (void *)TheSession);
+ session_new_modules(TheSession);
+
+ pthread_mutex_init(&TheSession->SessionMutex, NULL);
+ pthread_mutex_lock(&SessionListMutex);
+ TheSession->nonce = rand();
TheSession->next = SessionList;
+ TheSession->is_mobile = -1;
SessionList = TheSession;
- pthread_mutex_unlock(&MasterCritter);
- sprintf(str_session, "%d", TheSession->session_id);
- f = fork();
- fflush(stdout); fflush(stdin);
- if (f==0) {
- dup2(TheSession->inpipe[0], 0);
- dup2(TheSession->outpipe[1], 1);
- /* Close the ends of the pipes that we're not using */
- close(TheSession->inpipe[1]);
- close(TheSession->outpipe[0]);
- execlp("./webcit", "webcit", str_session, defaulthost,
- defaultport, NULL);
- printf("HTTP/1.0 404 WebCit Failure\n\n");
- printf("Server: %s\n", SERVER);
- printf("Content-type: text/html\n");
- printf("Content-length: 76\n");
- printf("\n");
- printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
- printf("<BODY>execlp() failed</BODY></HTML>\n");
- exit(0);
- }
- /* Close the ends of the pipes that we're not using */
- close(TheSession->inpipe[0]);
- close(TheSession->outpipe[1]);
- }
+ pthread_mutex_unlock(&SessionListMutex);
- /*
- * Send the request to the appropriate session...
- */
- printf(" Writing %d lines of command\n", num_lines);
- printf("%s\n", &req[0][0]);
- for (a=0; a<num_lines; ++a) {
- write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
- write(TheSession->inpipe[1], "\n", 1);
+ if (StrLength(Hdr->c_language) > 0) {
+ lprintf(9, "Session cookie requests language '%s'\n", ChrPtr(Hdr->c_language));
+ set_selected_language(ChrPtr(Hdr->c_language));
+ go_selected_language();
}
- printf(" Writing %d bytes of content\n", ContentLength);
- if (ContentLength > 0) {
- while (ContentLength > 0) {
- a = ContentLength;
- if (a > sizeof buf) a = sizeof buf;
- if (!client_read(sock, buf, a)) goto end;
- if (write(TheSession->inpipe[1], buf, a) != a) goto end;
- ContentLength -= a;
- }
-
- /* Discard the CRLF following the POST data. Yes, this is
- * necessary. No, you don't want to know why. */
- if (!client_read(sock, buf, 2)) goto end;
- }
+ }
/*
- * ...and get the response.
+ * A future improvement might be to check the session integrity
+ * at this point before continuing.
*/
- printf(" Reading response\n");
- ContentLength = 0;
- do {
- gets0(TheSession->outpipe[0], buf);
- write(sock, buf, strlen(buf));
- write(sock, "\n", 1);
- if (!strncasecmp(buf, "Content-length: ", 16))
- ContentLength = atoi(&buf[16]);
- if (!strcasecmp(buf, "X-WebCit-Session: close")) {
- CloseSession = 1;
- }
- } while (strlen(buf) > 0);
-
- printf(" Reading %d bytes of content\n", ContentLength);
- while(ContentLength--) {
- read(TheSession->outpipe[0], buf, 1);
- write(sock, buf, 1);
- }
/*
- * If the last response included a "close session" directive,
- * remove the context now.
+ * Bind to the session and perform the transaction
*/
- if (CloseSession) {
- printf("Removing session.\n");
- pthread_mutex_lock(&MasterCritter);
+ pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
+ pthread_setspecific(MyConKey, (void *)TheSession);
+
+ TheSession->lastreq = time(NULL); /* log */
+ TheSession->Hdr = Hdr;
+
+ session_attach_modules(TheSession);
+ session_loop(); /* do transaction */
- if (SessionList==TheSession) {
- SessionList = SessionList->next;
- }
- else {
- for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
- if (sptr->next == TheSession) {
- sptr->next = TheSession->next;
- }
- }
- }
- close(TheSession->inpipe[1]);
- close(TheSession->outpipe[0]);
- unlock_session(TheSession);
- free(TheSession);
+ /* How long did this transaction take? */
+ gettimeofday(&tx_finish, NULL);
- pthread_mutex_unlock(&MasterCritter);
- }
+ lprintf(9, "Transaction [%s] completed in %ld.%06ld seconds.\n",
+ ChrPtr(Hdr->this_page),
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000
+ );
+
+ session_detach_modules(TheSession);
+
+ TheSession->Hdr = NULL;
+ pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
+
+ http_destroy_modules(Hdr);
+}
+
+void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
+{
+ wcsession *WCC = WC;
+ StrBufAppendPrintf(Target, "%ld",
+ (WCC != NULL)? WCC->nonce:0);
+}
+
+void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
+{
+ StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
+}
+
+void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
+{
+ StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0);
+}
+
+void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.ContentLength = StrToi(Line);
+}
+
+void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.ContentType = Line;
+}
+
+void Header_HandleUserAgent(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.user_agent = Line;
+#ifdef TECH_PREVIEW
+/* TODO: do this later on session creating
+ if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {
+ WCC->is_mobile = 1;
+ }
else {
- end:
- unlock_session(TheSession);
- }
+ WCC->is_mobile = 0;
+ }
+*/
+#endif
+}
+
- /*
- * Now our HTTP connection is done. It would be relatively easy
- * to support HTTP/1.1 "persistent" connections by looping back to
- * the top of this function. For now, we'll just close.
- */
+void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ if ((follow_xff) && (hdr->HR.http_host != NULL))
+ return;
+ else
+ hdr->HR.http_host = Line;
+}
+
+void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ if (follow_xff)
+ hdr->HR.http_host = Line;
+}
+
+
+void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.browser_host = Line;
+
+ while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
+ StrBufRemove_token(hdr->HR.browser_host, 0, ',');
+ }
+ StrBufTrim(hdr->HR.browser_host);
+}
- free(req);
- printf(" Closing socket\n");
- close(sock);
+void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
+}
+void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
/*
- * The thread handling this HTTP connection is now finished.
+ * Can we compress?
*/
- pthread_exit(NULL);
+ if (strstr(&ChrPtr(Line)[16], "gzip")) {
+ hdr->HR.gzip_ok = 1;
}
+}
+const char *ReqStrs[eNONE] = {
+ "GET",
+ "POST",
+ "OPTIONS",
+ "PROPFIND",
+ "PUT",
+ "DELETE",
+ "HEAD",
+ "MOVE",
+ "COPY"
+};
+
+void
+ServerStartModule_CONTEXT
+(void)
+{
+ long *v;
+ HttpReqTypes = NewHash(1, NULL);
+ HttpHeaderHandler = NewHash(1, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eGET;
+ Put(HttpReqTypes, HKEY("GET"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = ePOST;
+ Put(HttpReqTypes, HKEY("POST"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eOPTIONS;
+ Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = ePROPFIND;
+ Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = ePUT;
+ Put(HttpReqTypes, HKEY("PUT"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eDELETE;
+ Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eHEAD;
+ Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eMOVE;
+ Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eCOPY;
+ Put(HttpReqTypes, HKEY("COPY"), v, NULL);
+}
+
+void
+ServerShutdownModule_CONTEXT
+(void)
+{
+ DeleteHash(&HttpReqTypes);
+ DeleteHash(&HttpHeaderHandler);
+}
+
+void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
+{
+ OneHttpHeader *pHdr;
+ pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
+ memset(pHdr, 0, sizeof(OneHttpHeader));
+ pHdr->H = F;
+ Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
+}
+
+
+void
+InitModule_CONTEXT
+(void)
+{
+ RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
+ RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
+ RegisterHeaderHandler(HKEY("USER-AGENT"), Header_HandleUserAgent);
+ RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost);
+ RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
+ RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
+ RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
+ RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
+
+ RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, CTX_NONE);
+ RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, CTX_NONE);
+ RegisterNamespace("NONCE", 0, 0, tmplput_nonce, 0);
+
+ WebcitAddUrlHandler(HKEY("404"), do_404, ANONYMOUS|COOKIEUNNEEDED);
+/*
+ * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
+ * Short-circuit these requests so we don't have to send them through the full processing loop.
+ */
+ WebcitAddUrlHandler(HKEY("scripts"), do_404, ANONYMOUS|BOGUS); /* /root.exe /* Worms and trojans and viruses, oh my! */
+ WebcitAddUrlHandler(HKEY("c"), do_404, ANONYMOUS|BOGUS); /* /winnt */
+ WebcitAddUrlHandler(HKEY("MSADC"), do_404, ANONYMOUS|BOGUS);
+ WebcitAddUrlHandler(HKEY("_vti"), do_404, ANONYMOUS|BOGUS); /* Broken Microsoft DAV implementation */
+ WebcitAddUrlHandler(HKEY("MSOffice"), do_404, ANONYMOUS|BOGUS); /* Stoopid MSOffice thinks everyone is IIS */
+ WebcitAddUrlHandler(HKEY("nonexistenshit"), do_404, ANONYMOUS|BOGUS); /* Exploit found in the wild January 2009 */
+}
+
+
+
+void
+HttpDestroyModule_CONTEXT
+(ParsedHttpHdrs *httpreq)
+{
+ FreeStrBuf(&httpreq->ReadBuf);
+ FreeStrBuf(&httpreq->PlainArgs);
+ FreeStrBuf(&httpreq->this_page);
+ DeleteHash(&httpreq->HTTPHeaders);
+
+}