#include "webcit.h"
+
+/*
+ * Display the login screen
+ */
+void display_login() {
+ char buf[256];
+
+ printf("HTTP/1.0 200 OK\n");
+ output_headers();
+
+ wprintf("<HTML><BODY>\n");
+ wprintf("<CENTER><TABLE border=0><TR><TD>\n");
+
+ /* FIX replace with the correct image */
+ wprintf("<IMG SRC=\"/static/velma.gif\">");
+ wprintf("</TD><TD><CENTER>\n");
+
+ serv_puts("MESG hello");
+ serv_gets(buf);
+ if (buf[0]=='1') fmout(NULL);
+
+ wprintf("</CENTER></TD></TR></TABLE></CENTER>\n");
+
+ wprintf("<HR>\n");
+ /* FIX add instructions here */
+ wprintf("<CENTER><FORM ACTION=\"/login\" METHOD=\"POST\">\n");
+ wprintf("<TABLE border><TR>\n");
+ wprintf("<TD>User Name:</TD>\n");
+ wprintf("<TD><INPUT TYPE=\"text\" NAME=\"name\" MAXLENGTH=\"25\">\n");
+ wprintf("</TD></TR><TR>\n");
+ wprintf("<TD>Password:</TD>\n");
+ wprintf("<TD><INPUT TYPE=\"password\" NAME=\"pass\" MAXLENGTH=\"20\"></TD>\n");
+ wprintf("</TR></TABLE>\n");
+ wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Login\">\n");
+ wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"New User\">\n");
+ wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Exit\">\n");
+ wprintf("</FORM></CENTER>\n");
+ wprintf("</BODY></HTML>\n");
+ wDumpContent();
+ }
+
+
+
+
/*
* This function needs to get called whenever a PASS or NEWU succeeds
*/
wprintf("</BODY></HTML>\n");
wDumpContent();
}
+
+
+void do_logout() {
+ char buf[256];
+
+ strcpy(wc_username, "");
+ strcpy(wc_password, "");
+ strcpy(wc_roomname, "");
+ strcpy(wc_host, "");
+ strcpy(wc_port, "");
+
+ printf("HTTP/1.0 200 OK\n");
+ output_headers();
+ printf("X-WebCit-Session: close\n");
+
+ wprintf("<HTML><HEAD><TITLE>Goodbye</TITLE></HEAD><BODY><CENTER>\n");
+
+ serv_puts("MESG goodbye");
+ serv_gets(buf);
+
+ if (buf[0]=='1') fmout(NULL);
+ else wprintf("Goodbye\n");
+
+ wprintf("</CENTER></BODY></HTML>\n");
+ wDumpContent();
+ serv_puts("QUIT");
+ exit(0);
+ }
struct wc_session *SessionList = NULL;
+/* Only one thread may manipulate SessionList at a time... */
+pthread_mutex_t MasterCritter;
+
int GenerateSessionID() {
return getpid();
}
struct wc_session *sptr;
struct wc_session *TheSession;
int ContentLength;
+ int CloseSession = 0;
printf("Reading request from socket %d\n", sock);
*/
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;
}
}
+ pthread_mutex_unlock(&MasterCritter);
}
/*
*/
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();
printf("<BODY>execlp() failed</BODY></HTML>\n");
exit(0);
}
+ pthread_mutex_unlock(&MasterCritter);
}
/*
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");
printf("Unlocking.\n");
pthread_mutex_unlock(&TheSession->critter);
+
+
+ /*
+ * If the last response included a "close session" directive,
+ * remove the context now.
+ */
+ if (CloseSession) {
+ printf("Removing session.\n");
+ pthread_mutex_lock(&MasterCritter);
+
+ if (SessionList==TheSession) {
+ SessionList = SessionList->next;
+ }
+ else {
+ for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
+ if (sptr->next == TheSession) {
+ sptr->next = TheSession->next;
+ }
+ }
+ }
+
+ free(TheSession);
+
+ pthread_mutex_unlock(&MasterCritter);
+ }
+
+
+
/*
* The thread handling this HTTP connection is now finished.
*/
wprintf("<LI><B><A HREF=\"/advanced\">\n");
wprintf("Advanced options</B></A><BR>...and maintenance</LI>\n");
- wprintf("<LI><B><A HREF=\"/termquit\">\n");
+ wprintf("<LI><B><A HREF=\"/termquit\" TARGET=\"_top\">\n");
wprintf("Log off</B></A><BR>Bye!</LI>\n");
wprintf("</UL>\n");
+++ /dev/null
-<HTML><BODY>
-<TABLE border=0><TR>
- <TD><IMG SRC="/static/velma.gif"></TD>
- <TD><H1>"Velma"
- </H1><H2>(next generation WebCit)</H2>
- Please log in...<BR>
- <FORM ACTION="/login" METHOD="POST">
- <TABLE border><TR>
- <TD>User Name:</TD>
- <TD><INPUT TYPE="text" NAME="name" MAXLENGTH="25"></TD>
- </TR><TR>
- <TD>Password:</TD>
- <TD><INPUT TYPE="password" NAME="pass" MAXLENGTH="20"></TD>
- </TR>
- </TABLE>
-
- <INPUT type="submit" NAME="action" VALUE="Login">
- <INPUT type="submit" NAME="action" VALUE="New User">
- <INPUT type="submit" NAME="action" VALUE="Exit">
- </FORM>
-
-</TD></TR></TABLE>
-</BODY></HTML>
}
else if (!logged_in) {
- output_static("login.html");
+ display_login();
}
/* Various commands... */
dotgoto();
}
- else if (!strncasecmp(cmd, "GET /test", 9)) {
- printf("HTTP/1.0 200 OK\n");
- output_headers();
-
- wprintf("<HTML><BODY>\n");
- wprintf("<H1>diagnostic page</H1>\n");
- wprintf("TransactionCount is %d<HR>\n", TransactionCount);
- wprintf("You're in session %d<BR>\n", wc_session);
- wprintf("Logged in as <em>"); escputs(wc_username);
- wprintf("</em><BR>\n");
- wprintf("</BODY></HTML>\n");
- wDumpContent();
+ else if (!strncasecmp(cmd, "GET /termquit", 13)) {
+ do_logout();
}
/* When all else fails... */
int msock; /* master listening socket */
extern void *context_loop(int);
+extern pthread_mutex_t MasterCritter;
/*
* This is a generic function to set up a master socket for listening on
msock = ig_tcp_server(PORT_NUM, 5);
printf("Listening on socket %d\n", msock);
+ pthread_mutex_init(&MasterCritter, NULL);
+
/*
* Endless loop. Listen on the master socket. When a connection
* comes in, create a socket, a context, and a thread.