[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[or-cvs] more robust http(ish) handling



Update of /home/or/cvsroot/src/or
In directory moria.seul.org:/home/arma/work/onion/cvs/src/or

Modified Files:
	buffers.c connection.c directory.c or.h 
Log Message:
more robust http(ish) handling


Index: buffers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/buffers.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- buffers.c	24 Aug 2002 04:59:21 -0000	1.8
+++ buffers.c	28 Sep 2002 05:53:00 -0000	1.9
@@ -129,9 +129,9 @@
 }
 
 int fetch_from_buf(char *string, int string_len,
-                 char **buf, int *buflen, int *buf_datalen) {
+                   char **buf, int *buflen, int *buf_datalen) {
 
-  /* if there is string_len bytes in buf, write them onto string,
+  /* if there are string_len bytes in buf, write them onto string,
    * then memmove buf back (that is, remove them from buf) */
 
   assert(string && buf && *buf && buflen && buf_datalen);
@@ -145,5 +145,27 @@
   *buf_datalen -= string_len;
   memmove(*buf, *buf+string_len, *buf_datalen);
   return *buf_datalen;
+}
+
+int find_on_inbuf(char *string, int string_len,
+                  char *buf, int buf_datalen) {
+  /* find first instance of needle 'string' on haystack 'buf'. return how
+   * many bytes from the beginning of buf to the end of string.
+   * If it's not there, return -1.
+   */
+
+  char *location;
+  char *last_possible = buf + buf_datalen - string_len;
+
+  assert(string && string_len > 0 && buf);
+
+  if(buf_datalen < string_len)
+    return -1;
+
+  for(location = buf; location <= last_possible; location++)
+    if((*location == *string) && !memcmp(location+1, string+1, string_len-1))
+      return location-buf+string_len;
+
+  return -1;
 }
 

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -d -r1.26 -r1.27
--- connection.c	28 Sep 2002 00:52:59 -0000	1.26
+++ connection.c	28 Sep 2002 05:53:00 -0000	1.27
@@ -352,6 +352,10 @@
   return fetch_from_buf(string, len, &conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen);
 }
 
+int connection_find_on_inbuf(char *string, int len, connection_t *conn) {
+  return find_on_inbuf(string, len, conn->inbuf, conn->inbuf_datalen);
+}
+
 int connection_wants_to_flush(connection_t *conn) {
   return conn->outbuf_flushlen;
 }

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/src/or/directory.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- directory.c	28 Sep 2002 00:52:59 -0000	1.3
+++ directory.c	28 Sep 2002 05:53:00 -0000	1.4
@@ -12,8 +12,10 @@
 
 static char the_directory[MAX_DIR_SIZE+1];
 static int directorylen=0;
+static int reading_headers=0;
 
-static char getstring[] = "GET / HTTP/1.0\n\r";
+static char getstring[] = "GET / HTTP/1.0\r\n\r\n";
+static char answerstring[] = "HTTP/1.0 200 OK\r\n\r\n";
 
 /********* END VARIABLES ************/
 
@@ -22,11 +24,14 @@
   struct sockaddr_in router_addr;
   int s;
 
-  log(LOG_DEBUG,"directory_initiate_fetch(): initiating directory fetch");
-
   if(!router) /* i guess they didn't have one in mind for me to use */
     return;
 
+  if(connection_get_by_type(CONN_TYPE_DIR)) /* there's already a fetch running */
+    return;
+
+  log(LOG_DEBUG,"directory_initiate_fetch(): initiating directory fetch");
+
   conn = connection_new(CONN_TYPE_DIR);
   if(!conn)
     return;
@@ -107,7 +112,7 @@
 void directory_rebuild(void) {
 
   dump_directory_to_string(the_directory, MAX_DIR_SIZE);
-  log(LOG_DEBUG,"New directory:\n'%s'",the_directory);  
+  log(LOG_DEBUG,"New directory:\n%s",the_directory);  
   directorylen = strlen(the_directory);
 
 }
@@ -155,6 +160,7 @@
 }
 
 int directory_handle_command(connection_t *conn) {
+  char buf[15];
 
   assert(conn && conn->type == CONN_TYPE_DIR);
 
@@ -163,7 +169,14 @@
     return 0; /* not yet */
   }
 
-  /* for now, don't bother reading it. */
+  if(connection_fetch_from_buf(buf,strlen(getstring),conn) < 0) {
+    return -1;    
+  }
+
+  if(strncasecmp(buf,getstring,strlen("GET / HTTP/"))) {
+    log(LOG_DEBUG,"directory_handle_command(): Command doesn't seem to be a get. Closing,");
+    return -1;
+  }
 
   if(directorylen == 0) {
     log(LOG_DEBUG,"directory_handle_command(): My directory is empty. Closing.");
@@ -171,7 +184,8 @@
   }
 
   log(LOG_DEBUG,"directory_handle_command(): Dumping directory to client."); 
-  if(connection_write_to_buf(the_directory, directorylen, conn) < 0) {
+  if((connection_write_to_buf(answerstring, strlen(answerstring), conn) < 0) ||
+     (connection_write_to_buf(the_directory, directorylen, conn) < 0)) {
     log(LOG_DEBUG,"directory_handle_command(): my outbuf is full. Oops.");
     return -1;
   }
@@ -182,9 +196,24 @@
 
 int directory_handle_reading(connection_t *conn) {
   int amt;
+  char *headers;
 
   assert(conn && conn->type == CONN_TYPE_DIR);
 
+  if(reading_headers) {
+    amt = connection_find_on_inbuf("\r\n\r\n", 4, conn);
+    if(amt < 0) /* not there yet */
+      return 0;
+    headers = malloc(amt+1);
+    if(connection_fetch_from_buf(headers,amt,conn) < 0) {
+      log(LOG_DEBUG,"directory_handle_reading(): fetch_from_buf failed (reading headers).");
+      return -1;
+    }
+    headers[amt] = 0; /* null terminate it, */
+    free(headers); /* and then throw it away */
+    reading_headers = 0;
+  }
+
   amt = conn->inbuf_datalen;
 
   if(amt + directorylen >= MAX_DIR_SIZE) {
@@ -196,7 +225,7 @@
     amt, directorylen);
 
   if(connection_fetch_from_buf(the_directory+directorylen,amt,conn) < 0) {
-    log(LOG_DEBUG,"directory_handle_reading(): fetch_from_buf failed.");
+    log(LOG_DEBUG,"directory_handle_reading(): fetch_from_buf failed (reading dir).");
     return -1;    
   }
 
@@ -233,6 +262,7 @@
     case DIR_CONN_STATE_SENDING_COMMAND:
       log(LOG_DEBUG,"connection_dir_finished_flushing(): client finished sending command.");
       directorylen = 0;
+      reading_headers = 1;
       conn->state = DIR_CONN_STATE_READING;
       connection_watch_events(conn, POLLIN);
       return 0;

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- or.h	28 Sep 2002 01:40:11 -0000	1.30
+++ or.h	28 Sep 2002 05:53:00 -0000	1.31
@@ -387,8 +387,16 @@
 
 int fetch_from_buf(char *string, int string_len,
 		                 char **buf, int *buflen, int *buf_datalen);
-	  /* if there is string_len bytes in buf, write them onto string,
-	  *    * then memmove buf back (that is, remove them from buf) */
+  /* if there is string_len bytes in buf, write them onto string,
+   * then memmove buf back (that is, remove them from buf)
+   */
+
+int find_on_inbuf(char *string, int string_len,
+                  char *buf, int buf_datalen);
+  /* find first instance of needle 'string' on haystack 'buf'. return how
+   * many bytes from the beginning of buf to the end of string.
+   * If it's not there, return -1.
+   */
 
 /********************************* cell.c ***************************/
 
@@ -463,6 +471,7 @@
 int connection_fetch_from_buf(char *string, int len, connection_t *conn);
 
 int connection_outbuf_too_full(connection_t *conn);
+int connection_find_on_inbuf(char *string, int len, connection_t *conn);
 int connection_wants_to_flush(connection_t *conn);
 int connection_flush_buf(connection_t *conn);