Hi,

This is a patch against the latest svn revision, fixing the
fixup_padding. This is the original implementation (was named
fixup_params function). Although works simpler, the alo's version was
not quite correct by leaving paddingLength field of the last FCGI record
to be blank (0x00) while this field must be filled with the number of
bytes of the pad. If this is not filled, then the server would take the
pad as the start of the next record, while it is an invalid record
(starting with 0x00), hence, the server would drop the connection.

This patch traverses the buffer and find all the records inside and fill
the last record's paddingLength with the pad size (if applicable).
Index: handler_fastcgi.c
===================================================================
--- handler_fastcgi.c	(revision 123)
+++ handler_fastcgi.c	(working copy)
@@ -149,7 +149,7 @@
 	return ret_ok;
 }
 
-
+#if 0
 static void
 fixup_padding (cherokee_buffer_t *buf, cuint_t id)
 {
@@ -173,8 +173,54 @@
 	cherokee_buffer_ensure_size (buf, buf->len + pad);
 	cherokee_buffer_add (buf, padding, pad);
 }
+#endif
 
+static void
+fixup_padding (cherokee_buffer_t *buf, cuint_t id)
+{
+  char *byte, *end, *last_pad;
+  char padding [8] = {0, 0, 0, 0, 0, 0, 0, 0};
+  int length;
+  int crafted_id [2];
+  int pad;
+  if (buf->len == 0)
+    return;
+  end = buf->buf + buf->len;
+  crafted_id [0] = (cuchar_t) id;
+  crafted_id [1] = (cuchar_t) (id >> 8) & 0xff;
+  byte = (char*) buf->buf;
+  while (byte < end)
+  {
+    byte += 2;
+    if (*byte == (char) 0xFF)
+      *byte = crafted_id [1];
+    byte ++;
+    if (*byte == (char) 0xFF)
+      *byte = crafted_id [0];
+    byte ++;
+    length = (*byte << 8);
+    byte ++;
+    length |= *byte;
+    byte ++;
+    length += *byte;
 
+    last_pad = byte;
+    byte ++;
+    byte += (length + 1);
+  }
+
+  if ((buf->len % 8) != 0)
+  {
+    pad = 8 - (buf->len % 8);
+    cherokee_buffer_ensure_size (buf, buf->len + pad);
+
+    *last_pad = pad;
+    cherokee_buffer_add (buf, padding, pad);
+  }
+
+}
+
+
 static void
 add_env_pair_with_id (cherokee_buffer_t *buf, cuint_t id,
 		      char *key, int key_len,
_______________________________________________
Cherokee mailing list
Cherokee@lists.alobbs.com
http://www.alobbs.com/cgi-bin/mailman/listinfo/cherokee

Reply via email to