Hi,

Please find some FastCGI fixes to the trunk as attached. What it does:

* Put the managers into a one static table for all threads so it will
not be re-created all the time.
* Put the connection to blocking mode, this was done to make debugging
easier. It could be turned off later.
* Added fcgi_phase_finished to mark the session has ended after received
end-request from the server.


Index: fcgi_manager.c
===================================================================
--- fcgi_manager.c	(revision 112)
+++ fcgi_manager.c	(working copy)
@@ -150,7 +150,7 @@
 	ret = cherokee_socket_connect (fcgim->socket);
 	fcgim->connected = (ret == ret_ok);
 	
-	cherokee_fd_set_nonblocking (fcgim->socket->socket);
+	/*cherokee_fd_set_nonblocking (fcgim->socket->socket);*/
 
 	return ret;
 }
@@ -278,6 +278,7 @@
 ret_t 
 cherokee_fcgi_manager_unregister_conn (cherokee_fcgi_manager_t *fcgim, cherokee_connection_t *conn)
 {
+  ret_t ret = ret_error;
 	int i;
 
 	/* Look for the connection reference
@@ -285,13 +286,14 @@
 	for (i=0; i<fcgim->conn_poll_size; i++) {
 		if (fcgim->conn_poll[i] == conn) {
 			fcgim->conn_poll[i] = NULL;
-			return ret_ok;
+      ret = ret_ok;
+			break;
 		}
 	}
 
-	TRACE (ENTRIES, "Manager unregistered %s\n", "");
+	TRACE (ENTRIES, "Manager unregistered\n", "");
 
-	return ret_error;
+	return ret;
 }
 
 
@@ -317,20 +319,19 @@
 	return ret_ok;
 }
 
-/* static void */
-/* set_status (cherokee_fcgi_manager_t *fcgim, cherokee_fcgi_status_t status) */
-/* { */
-/* 	cherokee_handler_fastcgi_t *fcgi; */
-/* 	cherokee_connection_t *conn; */
-  
-/* 	conn = fcgim->conn_poll [fcgim->request_id - 1]; */
-/* 	if (conn != NULL) { */
-/* 		fcgi = (cherokee_handler_fastcgi_t *) conn->handler; */
-/* 		if (fcgi != NULL) { */
-/* 			fcgi->status = status; */
-/* 		} */
-/* 	} */
-/* } */
+static void
+set_status (cherokee_fcgi_manager_t *fcgim, cherokee_fcgi_phase_t status)
+{
+	cherokee_handler_fastcgi_t *fcgi;
+	cherokee_connection_t *conn;
+	conn = fcgim->conn_poll [fcgim->request_id - 1];
+	if (conn != NULL) {
+		fcgi = (cherokee_handler_fastcgi_t *) conn->handler;
+		if (fcgi != NULL) {
+			fcgi->phase = status;
+		}
+	}
+}
 
 static void
 process_buffer (cherokee_fcgi_manager_t *fcgim, void *data, cuint_t data_len)
@@ -357,8 +358,9 @@
 
 	case FCGI_STDOUT:
 		cherokee_buffer_add (&fcgi->incoming_buffer, data, data_len);
-/* 		if (fcgim->remaining_size == 0) */
-/* 			set_status (fcgim, fcgi_data_available); */
+    /*cherokee_buffer_print_debug (&fcgi->incoming_buffer, -1);*/
+ 		/*if (fcgim->remaining_size == 0)
+			set_status (fcgim,);*/
 		break;
 	}
 }
@@ -437,17 +439,20 @@
 		switch (fcgim->request_type) {
 		case FCGI_STDERR:
 		case FCGI_STDOUT:
+      ret = ret_ok;
 			if (len > 0)
 				process_buffer (fcgim, (start + offset), len);
 			break;
 
 		case FCGI_END_REQUEST:
+      ret = ret_ok;
 			end_request = (FCGI_EndRequestBody *) (start + offset);
 			fcgim->status = end_request->protocolStatus;
 			fcgim->return_value =  end_request->appStatusB0        | 
 				(end_request->appStatusB0 << 8)  | 
 				(end_request->appStatusB0 << 16) | 
 				(end_request->appStatusB0 << 24);
+      set_status (fcgim, fcgi_phase_finished);
 /* 			set_status (fcgim, fcgi_data_completed); */        // TODO !!!!
 			break;
 
@@ -472,7 +477,7 @@
 ret_t 
 fcgi_manager_step (cherokee_fcgi_manager_t *fcgim, cuint_t id)
 {
-	ret_t                        ret;
+	ret_t                        ret = ret_eagain;
 	size_t                       size;
 	cherokee_connection_t       *conn;
 	cherokee_handler_fastcgi_t  *fcgi;
@@ -492,6 +497,8 @@
 		 */
 		if (fcgim->read_buffer.len < sizeof(FCGI_Header)) {
 			ret = cherokee_socket_read (fcgim->socket, &fcgim->read_buffer, DEFAULT_READ_SIZE, &size);
+      TRACE (ENTRIES, "Manager readed ID=%d ret=%d size=%d\n", id, ret, size);
+      /*cherokee_buffer_print_debug (&fcgim->read_buffer, -1);*/
 			switch (ret) {
 			case ret_ok:
 				break;
@@ -504,8 +511,6 @@
 				RET_UNKNOWN(ret);
 				return ret_error;
 			}
-
-			TRACE (ENTRIES, "Manager readed ID=%d ret=%d size=%d\n", id, ret, size);
 		}
 
 		/* Process the information
Index: thread.c
===================================================================
--- thread.c	(revision 112)
+++ thread.c	(working copy)
@@ -46,6 +46,7 @@
 #define DEBUG_BUFFER(b)  fprintf(stderr, "%s:%d len=%d crc=%d\n", __FILE__, __LINE__, b->len, cherokee_buffer_crc32(b))
 #define ENTRIES "core,thread"
 
+static 	cherokee_table_t       *__fastcgi_managers = NULL;
 
 static char *
 phase_to_str (cherokee_connection_phase_t phase)
@@ -245,7 +246,8 @@
 
 	/* FastCGI managers
 	 */
-	cherokee_table_init (&n->fastcgi_managers);
+  if (__fastcgi_managers == NULL)
+	  cherokee_table_new (&__fastcgi_managers);
 	
 	/* Return the object
 	 */
@@ -1097,9 +1099,9 @@
 	CHEROKEE_MUTEX_DESTROY (&thd->ownership);
 
 	/* FastCGI managers
-	 */
+	 * /
 	cherokee_table_mrproper2 (&thd->fastcgi_managers, 
-				  (cherokee_table_free_item_t) cherokee_fcgi_manager_free);
+				  (cherokee_table_free_item_t) cherokee_fcgi_manager_free);*/
 
 	free (thd);
 	return ret_ok;
@@ -1705,3 +1707,13 @@
 
 	return ret_ok;
 }
+
+ret_t 
+cherokee_thread_get_fcgi_managers (cherokee_thread_t *thd, cherokee_table_t **table)
+{
+  *table = __fastcgi_managers;
+  if (__fastcgi_managers == NULL)
+    return ret_not_found;
+  else
+    return ret_ok;
+}
Index: thread.h
===================================================================
--- thread.h	(revision 112)
+++ thread.h	(working copy)
@@ -85,8 +85,6 @@
 		uint32_t        recalculate;		
 	} accept;
 
-	cherokee_table_t        fastcgi_managers;
-
 } cherokee_thread_t;
 
 #define THREAD(x)         ((cherokee_thread_t *)(x))
@@ -121,5 +119,6 @@
 
 int   cherokee_thread_connection_num             (cherokee_thread_t *thd);
 ret_t cherokee_thread_close_all_connections      (cherokee_thread_t *thd);
+ret_t cherokee_thread_get_fcgi_managers          (cherokee_thread_t *thd, cherokee_table_t **table);
 
 #endif /* CHEROKEE_THREAD_H */
Index: handler_fastcgi.c
===================================================================
--- handler_fastcgi.c	(revision 112)
+++ handler_fastcgi.c	(working copy)
@@ -131,6 +131,7 @@
 ret_t 
 cherokee_handler_fastcgi_free (cherokee_handler_fastcgi_t *hdl)
 {
+  TRACE (ENTRIES, "Cleaning up\n", "");
 	cherokee_fcgi_manager_unregister_conn (hdl->manager_ref, HANDLER_CONN(hdl));
 
 	cherokee_buffer_mrproper (&hdl->data);
@@ -370,7 +371,9 @@
 	ret_t                    ret;
 	cherokee_connection_t   *conn      = HANDLER_CONN(fcgi);
 	cherokee_thread_t       *thread    = HANDLER_THREAD(fcgi);
-	cherokee_table_t        *managers  = &thread->fastcgi_managers;
+	cherokee_table_t        *managers;
+
+  cherokee_thread_get_fcgi_managers (thread, &managers);
 	
 	/* Read the current server and set the next one
 	 */
@@ -382,6 +385,7 @@
 	/* FastCGI manager
 	 */
 	ret = cherokee_table_get (managers, fcgi->configuration->host.buf, (void **)&fcgi->manager_ref);
+  TRACE (ENTRIES, "Checking manager: %p %d\n", managers, ret);
 	if (ret == ret_not_found) {
 		cherokee_fcgi_manager_t *n;
  
@@ -390,7 +394,7 @@
 		ret = cherokee_fcgi_manager_new (&n, fcgi->configuration);
 		if (unlikely (ret != ret_ok)) return ret;
 
-		TRACE (ENTRIES, "Creating new manager \"%s\"\n", fcgi->configuration->host.buf);
+		TRACE (ENTRIES, "Creating new manager \"%s\" %p\n", fcgi->configuration->host.buf, n);
 		
 		/* Assign the object to that path
 		 */
@@ -462,6 +466,9 @@
 
 	/* Read info from the FastCGI
 	 */
+  if (fcgi->phase == fcgi_phase_finished)
+    return ret_ok;
+
 	ret = cherokee_fcgi_manager_step (fcgim, fcgi->id);
 	switch (ret) {
 	case ret_error:
@@ -648,6 +655,7 @@
 
 	TRACE (ENTRIES, "Sending: there are %d bytes to be sent\n", fcgi->write_buffer.len);
 	
+  cherokee_buffer_print_debug (&fcgi->write_buffer, -1);
 	ret = cherokee_fcgi_manager_send (fcgi->manager_ref, &fcgi->write_buffer, &size);
 
 	TRACE (ENTRIES, "Sending: now, there are %d bytes, had ret=%d\n", fcgi->write_buffer.len, ret);
@@ -801,6 +809,7 @@
 		tmp = end + (*end == 0 ? 0 : 2);
 	}
 
+  TRACE (ENTRIES, "Header processed\n", "");
 	return ret_ok;
 }
 
@@ -870,6 +879,7 @@
 		if (cherokee_buffer_is_empty (&fcgi->incoming_buffer))
 			return ret_error;
 			
+    TRACE (ENTRIES, "Adding headers, phase %s\n", "chopping headers");
 		/* Look the end of headers
 		 */ 
 		content = strstr (fcgi->incoming_buffer.buf, CRLF CRLF);
@@ -919,11 +929,14 @@
 
 	/* Lets read from the FastCGI server
 	 */
+  TRACE (ENTRIES, "Step, phase %s\n", "read fcgi");
 	ret = read_fcgi (fcgi); 
 	switch (ret) {
 	case ret_ok:
-		cherokee_buffer_add_buffer (buffer, &fcgi->incoming_buffer);
-		cherokee_buffer_mrproper (&fcgi->incoming_buffer);
+    if (fcgi->incoming_buffer.len > 0) {
+      cherokee_buffer_add_buffer (buffer, &fcgi->incoming_buffer);
+      cherokee_buffer_mrproper (&fcgi->incoming_buffer);
+    }
 		return ret_ok;
 	case ret_error:
 	case ret_eagain:
Index: handler_fastcgi.h
===================================================================
--- handler_fastcgi.h	(revision 112)
+++ handler_fastcgi.h	(working copy)
@@ -67,7 +67,8 @@
 	fcgi_phase_init,
 	fcgi_phase_send_post,
 	fcgi_phase_send_header,
-	fcgi_phase_read_fcgi
+	fcgi_phase_read_fcgi,
+  fcgi_phase_finished,
 } cherokee_fcgi_phase_t;
 
 
_______________________________________________
Cherokee mailing list
Cherokee@lists.alobbs.com
http://www.alobbs.com/cgi-bin/mailman/listinfo/cherokee

Reply via email to