httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dean Gaudet <dgau...@arctic.org>
Subject Re: [PATCH] CGI unbuffering for 1.3
Date Tue, 08 Jul 1997 00:57:45 GMT
So anyhow, this is where I'm heading with Sameer's code.  This hasn't
even been tested yet, so don't bother patching it in.

- clean up spawn_child_err{,_buff} moving shared code into a _core routine

- fix a possible fd leak in spawn_child_err, if fdopen() failed it would
  lose a descriptor or three

- remove the fb->foobar tweaking in send_fb_length by enhancing bread() to
  deal with a partially filled buffer while in unbuffered mode

- remove the select() from send_fb_length, because as is it's not required
    the bread will block anyhow since it's a blocking fd.  This is going
    to change, I need select() and some more garbage in here to deal with
    a client disappearing while the CGI is off thinking.

- do the shared code thing with scan_script_header_err{,_buff}

That satisfy the complaints about it?  It's still ugly to have both BUFF *
and FILE * crap.  I'd really love to lose both in favour of sfio.

Dean

Index: alloc.c
===================================================================
RCS file: /export/home/cvs/apache/src/alloc.c,v
retrieving revision 1.36
diff -u -r1.36 alloc.c
--- alloc.c	1997/07/06 05:37:18	1.36
+++ alloc.c	1997/07/08 01:19:03
@@ -1025,10 +1025,16 @@
 #define enc_pipe(fds) pipe(fds)
 #endif /* WIN32 */
 
+/* for fdopen, to get binary mode */
+#if defined (__EMX__) || defined (WIN32)
+#define BINMODE	"b"
+#else
+#define BINMODE
+#endif
 
-int spawn_child_err_buff (pool *p, int (*func)(void *), void *data,
-			  enum kill_conditions kill_how,
-			  BUFF **pipe_in, BUFF **pipe_out, BUFF **pipe_err)
+static int spawn_child_err_core (pool *p, int (*func)(void *), void *data,
+		     enum kill_conditions kill_how,
+		     int *pipe_in, int *pipe_out, int *pipe_err)
 {
   int pid;
   int in_fds[2];
@@ -1036,13 +1042,7 @@
   int err_fds[2];
   int save_errno;
 
-  block_alarms();
-  
-  if (pipe_in && enc_pipe (in_fds) < 0)
-  {
-      save_errno = errno;
-      unblock_alarms();
-      errno = save_errno;
+  if (pipe_in && enc_pipe (in_fds) < 0) {
       return 0;
   }
   
@@ -1051,7 +1051,6 @@
     if (pipe_in) {
       close (in_fds[0]); close (in_fds[1]);
     }
-    unblock_alarms();
     errno = save_errno;
     return 0;
   }
@@ -1064,7 +1063,6 @@
     if (pipe_out) {
       close (out_fds[0]); close (out_fds[1]);
     }
-    unblock_alarms();
     errno = save_errno;
     return 0;
   }
@@ -1120,20 +1118,14 @@
       if(pid)
       {
           note_subprocess(p, pid, kill_how);
-          if(pipe_in)
-          {
-              *pipe_in = bcreate(p, B_WR);
-	      bpushfd(*pipe_in, in_fds[1], in_fds[1]);
+          if(pipe_in) {
+	      *pipe_in = in_fds[1];
           }
-          if(pipe_out)
-          {
-              *pipe_out = bcreate(p, B_RD);
-	      bpushfd(*pipe_out, out_fds[0], out_fds[0]);
+          if(pipe_out) {
+	      *pipe_out = out_fds[0];
           }
-          if(pipe_err)
-          {
-              *pipe_err = bcreate(p, B_RD);
-	      bpushfd(*pipe_err, err_fds[0], err_fds[0]);
+          if(pipe_err) {
+              *pipe_err = err_fds[0];
           }
       }
       SetThreadPriority(thread_handle, old_priority);
@@ -1157,7 +1149,6 @@
     if (pipe_err) {
       close (err_fds[0]); close (err_fds[1]);
     }
-    unblock_alarms();
     errno = save_errno;
     return 0;
   }
@@ -1196,237 +1187,106 @@
   
   if (pipe_out) {
     close (out_fds[1]);
-    *pipe_out = bcreate(p, B_RD);
-    bpushfd(*pipe_out, out_fds[0], out_fds[0]);
+    *pipe_out = out_fds[0];
   }
 
   if (pipe_in) {
     close (in_fds[0]);
-    *pipe_in = bcreate(p, B_WR);
-    bpushfd(*pipe_in, in_fds[1], in_fds[1]);
+    *pipe_in = in_fds[1];
   }
 
   if (pipe_err) {
     close (err_fds[1]);
-    *pipe_err = bcreate(p, B_RD);
-    bpushfd(*pipe_err, err_fds[0], err_fds[0]);
+    *pipe_err = err_fds[0];
   }
 #endif /* WIN32 */
 
-  unblock_alarms();
   return pid;
 }
 
+
 int spawn_child_err (pool *p, int (*func)(void *), void *data,
 		     enum kill_conditions kill_how,
 		     FILE **pipe_in, FILE **pipe_out, FILE **pipe_err)
 {
-  int pid;
-  int in_fds[2];
-  int out_fds[2];
-  int err_fds[2];
-  int save_errno;
+    int fd_in, fd_out, fd_err;
+    int pid, save_errno;
 
-  block_alarms();
-  
-  if (pipe_in && enc_pipe (in_fds) < 0)
-  {
-      save_errno = errno;
-      unblock_alarms();
-      errno = save_errno;
-      return 0;
-  }
-  
-  if (pipe_out && enc_pipe (out_fds) < 0) {
-    save_errno = errno;
-    if (pipe_in) {
-      close (in_fds[0]); close (in_fds[1]);
-    }
-    unblock_alarms();
-    errno = save_errno;
-    return 0;
-  }
+    block_alarms();
 
-  if (pipe_err && enc_pipe (err_fds) < 0) {
-    save_errno = errno;
-    if (pipe_in) {
-      close (in_fds[0]); close (in_fds[1]);
+    pid = spawn_child_err_core (p, func, data, kill_how,
+	    pipe_in ? &fd_in : NULL,
+	    pipe_out ? &fd_out : NULL,
+	    pipe_err ? &fd_err : NULL );
+
+    if (pid == 0) {
+	save_errno = errno;
+	unblock_alarms();
+	errno = save_errno;
+	return 0;
     }
+
     if (pipe_out) {
-      close (out_fds[0]); close (out_fds[1]);
+	*pipe_out = fdopen (fd_out, "r" BINMODE);
+	if (*pipe_out) note_cleanups_for_file (p, *pipe_out);
+	else close (fd_out);
     }
-    unblock_alarms();
-    errno = save_errno;
-    return 0;
-  }
 
-#ifdef WIN32
+    if (pipe_in) {
+	*pipe_in = fdopen (fd_in, "w" BINMODE);
+	if (*pipe_in) note_cleanups_for_file (p, *pipe_in);
+	else close (fd_in);
+    }
 
-  {
-      HANDLE thread_handle;
-      int hStdIn, hStdOut, hStdErr;
-      int old_priority;
-      
-      (void)acquire_mutex(spawn_mutex);
-      thread_handle = GetCurrentThread(); /* doesn't need to be closed */
-      old_priority = GetThreadPriority(thread_handle);
-      SetThreadPriority(thread_handle, THREAD_PRIORITY_HIGHEST);
-      /* Now do the right thing with your pipes */
-      if(pipe_in)
-      {
-          hStdIn = dup(fileno(stdin));
-          dup2(in_fds[0], fileno(stdin));
-          close(in_fds[0]);
-      }
-      if(pipe_out)
-      {
-          hStdOut = dup(fileno(stdout));
-          dup2(out_fds[1], fileno(stdout));
-          close(out_fds[1]);
-      }
-      if(pipe_err)
-      {
-          hStdErr = dup(fileno(stderr));
-          dup2(err_fds[1], fileno(stderr));
-          close(err_fds[1]);
-      }
+    if (pipe_err) {
+	*pipe_err = fdopen (fd_err, "r" BINMODE);
+	if (*pipe_err) note_cleanups_for_file (p, *pipe_err);
+	else close (fd_err);
+    }
 
-      pid = (*func)(data);
-      if(!pid)
-      {
-          save_errno = errno;
-          close(in_fds[1]);
-          close(out_fds[0]);
-          close(err_fds[0]);
-      }
+    unblock_alarms();
+    return pid;
+}
 
-      /* restore the original stdin, stdout and stderr */
-      if(pipe_in)
-          dup2(hStdIn, fileno(stdin));
-      if(pipe_out)
-          dup2(hStdOut, fileno(stdout));
-      if(pipe_err)
-          dup2(hStdErr, fileno(stderr));
 
-      if(pid)
-      {
-          note_subprocess(p, pid, kill_how);
-          if(pipe_in)
-          {
-              *pipe_in = fdopen(in_fds[1], "wb");
-              if(*pipe_in)
-                  note_cleanups_for_file(p, *pipe_in);
-          }
-          if(pipe_out)
-          {
-              *pipe_out = fdopen(out_fds[0], "rb");
-              if(*pipe_out)
-                  note_cleanups_for_file(p, *pipe_out);
-          }
-          if(pipe_err)
-          {
-              *pipe_err = fdopen(err_fds[0], "rb");
-              if(*pipe_err)
-                  note_cleanups_for_file(p, *pipe_err);
-          }
-      }
-      SetThreadPriority(thread_handle, old_priority);
-      (void)release_mutex(spawn_mutex);
-      /*
-       * go on to the end of the function, where you can
-       * unblock alarms and return the pid
-       */
+int spawn_child_err_buff (pool *p, int (*func)(void *), void *data,
+			  enum kill_conditions kill_how,
+			  BUFF **pipe_in, BUFF **pipe_out, BUFF **pipe_err)
+{
+    int fd_in, fd_out, fd_err;
+    int pid, save_errno;
 
-  }
-#else
+    block_alarms();
 
-  if ((pid = fork()) < 0) {
-    save_errno = errno;
-    if (pipe_in) {
-      close (in_fds[0]); close (in_fds[1]);
+    pid = spawn_child_err_core (p, func, data, kill_how,
+	    pipe_in ? &fd_in : NULL,
+	    pipe_out ? &fd_out : NULL,
+	    pipe_err ? &fd_err : NULL );
+
+    if (pid == 0) {
+	save_errno = errno;
+	unblock_alarms();
+	errno = save_errno;
+	return 0;
     }
+  
     if (pipe_out) {
-      close (out_fds[0]); close (out_fds[1]);
-    }
-    if (pipe_err) {
-      close (err_fds[0]); close (err_fds[1]);
-    }
-    unblock_alarms();
-    errno = save_errno;
-    return 0;
-  }
-
-  if (!pid) {
-    /* Child process */
-    
-    if (pipe_out) {
-      close (out_fds[0]);
-      dup2 (out_fds[1], STDOUT_FILENO);
-      close (out_fds[1]);
+	*pipe_out = bcreate(p, B_RD);
+	bpushfd(*pipe_out, fd_out, fd_out);
     }
 
     if (pipe_in) {
-      close (in_fds[1]);
-      dup2 (in_fds[0], STDIN_FILENO);
-      close (in_fds[0]);
+	*pipe_in = bcreate(p, B_WR);
+	bpushfd(*pipe_in, fd_in, fd_in);
     }
 
     if (pipe_err) {
-      close (err_fds[0]);
-      dup2 (err_fds[1], STDERR_FILENO);
-      close (err_fds[1]);
+	*pipe_err = bcreate(p, B_RD);
+	bpushfd(*pipe_err, fd_err, fd_err);
     }
 
-    /* HP-UX SIGCHLD fix goes here, if someone will remind me what it is... */
-    signal (SIGCHLD, SIG_DFL);	/* Was that it? */
-    
-    func (data);
-    exit (1);		/* Should only get here if the exec in func() failed */
-  }
-
-  /* Parent process */
-
-  note_subprocess (p, pid, kill_how);
-  
-  if (pipe_out) {
-    close (out_fds[1]);
-#ifdef __EMX__
-    /* Need binary mode set for OS/2. */
-    *pipe_out = fdopen (out_fds[0], "rb");
-#else
-    *pipe_out = fdopen (out_fds[0], "r");
-#endif  
-  
-    if (*pipe_out) note_cleanups_for_file (p, *pipe_out);
-  }
-
-  if (pipe_in) {
-    close (in_fds[0]);
-#ifdef __EMX__
-    /* Need binary mode set for OS/2 */
-    *pipe_in = fdopen (in_fds[1], "wb");
-#else
-    *pipe_in = fdopen (in_fds[1], "w");
-#endif
-    
-    if (*pipe_in) note_cleanups_for_file (p, *pipe_in);
-  }
-
-  if (pipe_err) {
-    close (err_fds[1]);
-#ifdef __EMX__
-    /* Need binary mode set for OS/2. */
-    *pipe_err = fdopen (err_fds[0], "rb");
-#else
-    *pipe_err = fdopen (err_fds[0], "r");
-#endif
-  
-    if (*pipe_err) note_cleanups_for_file (p, *pipe_err);
-  }
-#endif /* WIN32 */
-
-  unblock_alarms();
-  return pid;
+    unblock_alarms();
+    return pid;
 }
 
 static void free_proc_chain (struct process_chain *procs)
Index: buff.c
===================================================================
RCS file: /export/home/cvs/apache/src/buff.c,v
retrieving revision 1.35
diff -u -r1.35 buff.c
--- buff.c	1997/06/30 21:16:17	1.35
+++ buff.c	1997/07/08 01:19:06
@@ -419,12 +419,12 @@
 {
     if (value) {
 	fb->flags |= flag;
-	if( flag & B_CHUNK ) {
+	if (flag & B_CHUNK) {
 	    start_chunk(fb);
 	}
     } else {
 	fb->flags &= ~flag;
-	if( flag & B_CHUNK ) {
+	if (flag & B_CHUNK) {
 	    end_chunk(fb);
 	}
     }
@@ -526,9 +526,16 @@
     if (fb->flags & B_RDERR) return -1;
     if (nbyte == 0) return 0;
 
-    if (!(fb->flags & B_RD))
-    {
-/* Unbuffered reading */
+    if (!(fb->flags & B_RD)) {
+	/* Unbuffered reading.  First check if there was something in the
+	 * buffer from before we went unbuffered. */
+	if (fb->incnt) {
+	    i = (fb->incnt > nbyte) ? nbyte : fb->incnt;
+	    memcpy (buf, fb->inptr, i);
+	    fb->incnt -= i;
+	    fb->inptr += i;
+	    return i;
+	}
 	i = saferead( fb, buf, nbyte );
 	if (i == -1 && errno != EAGAIN) doerror(fb, B_RD);
 	return i;
Index: http_protocol.c
===================================================================
RCS file: /export/home/cvs/apache/src/http_protocol.c,v
retrieving revision 1.133
diff -u -r1.133 http_protocol.c
--- http_protocol.c	1997/07/06 05:37:19	1.133
+++ http_protocol.c	1997/07/08 01:19:11
@@ -1516,6 +1516,7 @@
 
     return (chunk_start + len_read);
 }
+
 long send_fb_length(BUFF *fb, request_rec *r, long length)
 {
     char buf[IOBUFSIZE];
@@ -1527,33 +1528,16 @@
 
     total_bytes_sent = 0;
 
-    /* Clear out the buffer */
-    if(fb->incnt != 0)
-      {
-	int o;
-	o = bwrite(c->client, fb->inptr, fb->incnt);
-	fb->incnt -= o;
-	fb->inptr += o;
-	total_bytes_sent += o;
-      }
-
     /* Make unbuffered */
-    fb->flags &= ~B_RD;
-
-    while(1)
-      {
-	fd_set fds;
+    bsetflag (fb, B_RD, 0);
 
+    while(1) {
 	bflush(c->client);
 
-	FD_ZERO(&fds);
-	FD_SET(fb->fd_in, &fds);
-
-	select(FD_SETSIZE, &fds, NULL, NULL, NULL);
-
 	while((n = bread(fb, buf, IOBUFSIZE)) < 1
-	      && fb->flags & B_ERROR && errno == EINTR)
-	  continue;
+	      && fb->flags & B_ERROR && errno == EINTR) {
+	    /* no-op */
+	}
 	
 	if (n < 1) {
             break;
Index: util_script.c
===================================================================
RCS file: /export/home/cvs/apache/src/util_script.c,v
retrieving revision 1.63
diff -u -r1.63 util_script.c
--- util_script.c	1997/07/06 05:37:20	1.63
+++ util_script.c	1997/07/08 01:19:13
@@ -312,7 +312,9 @@
     }
 }
 
-int scan_script_header_err_buff(request_rec *r, BUFF *fb, char *buffer)
+
+static int scan_script_header_err_core (request_rec *r, char *buffer,
+    int (*getsfunc)(char *, int, void *), void *getsfunc_data)
 {
     char x[MAX_STRING_LEN];
     char *w, *l;
@@ -325,7 +327,8 @@
     
     while(1) {
 
-	if (bgets(w, MAX_STRING_LEN-1, fb) <= 0) {
+	if ((*getsfunc)(w, MAX_STRING_LEN-1, getsfunc_data) == 0) {
+	    kill_timeout (r);
 	    log_reason ("Premature end of script headers", r->filename, r);
 	    return SERVER_ERROR;
         }
@@ -353,7 +356,7 @@
 
 	    if (!buffer)
 	      /* Soak up all the script output --- may save an outright kill */
-	      while (bgets(w, MAX_STRING_LEN-1, fb) <= 0)
+	      while ((*getsfunc)(w, MAX_STRING_LEN-1, getsfunc_data))
 		continue;
 	    
 	    kill_timeout (r);
@@ -401,95 +404,27 @@
     }
 }
 
-int scan_script_header_err(request_rec *r, FILE *f, char *buffer)
-{
-    char x[MAX_STRING_LEN];
-    char *w, *l;
-    int p;
-
-    if (buffer) *buffer = '\0';
-    w = buffer ? buffer : x;
-
-    hard_timeout ("read script header", r);
-    
-    while(1) {
-
-	if (fgets(w, MAX_STRING_LEN-1, f) == NULL) {
-	    kill_timeout (r);
-	    log_reason ("Premature end of script headers", r->filename, r);
-	    return SERVER_ERROR;
-        }
-
-	/* Delete terminal (CR?)LF */
-	
-	p = strlen(w);
-	if (p > 0 && w[p-1] == '\n')
-	{
-	    if (p > 1 && w[p-2] == '\015') w[p-2] = '\0';
-	    else w[p-1] = '\0';
-	}
-
-        if(w[0] == '\0') {
-	    kill_timeout (r);
-	    return OK;
-	}
-                                   
-	/* if we see a bogus header don't ignore it. Shout and scream */
-	
-        if(!(l = strchr(w,':'))) {
-	    char malformed[(sizeof MALFORMED_MESSAGE)+1+MALFORMED_HEADER_LENGTH_TO_SHOW];
-            strcpy(malformed, MALFORMED_MESSAGE);
-            strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
 
-	    if (!buffer)
-	      /* Soak up all the script output --- may save an outright kill */
-	      while (fgets(w, MAX_STRING_LEN-1, f) != NULL)
-		continue;
-	    
-	    kill_timeout (r);
-	    log_reason (malformed, r->filename, r);
-	    return SERVER_ERROR;
-        }
+static int getsfunc_FILE (char *buf, int len, void *f)
+{
+    return fgets (buf, len, (FILE *)f) != NULL;
+}
 
-        *l++ = '\0';
-	while (*l && isspace (*l)) ++l;
-	
-        if(!strcasecmp(w,"Content-type")) {
+int scan_script_header_err(request_rec *r, FILE *f, char *buffer)
+{
+    return scan_script_header_err_core (r, buffer, getsfunc_FILE, f);
+}
 
-	    /* Nuke trailing whitespace */
-	    
-	    char *endp = l + strlen(l) - 1;
-	    while (endp > l && isspace(*endp)) *endp-- = '\0';
-	    
-	    r->content_type = pstrdup (r->pool, l);
-	}
-        else if(!strcasecmp(w,"Status")) {
-            sscanf(l, "%d", &r->status);
-            r->status_line = pstrdup(r->pool, l);
-        }
-        else if(!strcasecmp(w,"Location")) {
-	    table_set (r->headers_out, w, l);
-        }   
-        else if(!strcasecmp(w,"Content-Length")) {
-	    table_set (r->headers_out, w, l);
-        }   
-        else if(!strcasecmp(w,"Transfer-Encoding")) {
-	    table_set (r->headers_out, w, l);
-        }   
+static int getsfunc_BUFF (char *w, int len, void *fb)
+{
+    return bgets (w, len, (BUFF *)fb) > 0;
+}
 
-/* The HTTP specification says that it is legal to merge duplicate
- * headers into one.  Some browsers that support Cookies don't like
- * merged headers and prefer that each Set-Cookie header is sent
- * separately.  Lets humour those browsers.
- */
-	else if(!strcasecmp(w, "Set-Cookie")) {
-	    table_add(r->err_headers_out, w, l);
-	}
-        else {
-	    table_merge (r->err_headers_out, w, l);
-        }
-    }
+int scan_script_header_err_buff(request_rec *r, BUFF *fb, char *buffer)
+{
+    return scan_script_header_err_core (r, buffer, getsfunc_BUFF, fb);
 }
+
 
 void send_size(size_t size, request_rec *r) {
     char ss[20];


Mime
View raw message