src/lib/net.c 1.75

1.75 2009/10/20 22:39:47 galt
keep http request together
Index: src/lib/net.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/net.c,v
retrieving revision 1.74
retrieving revision 1.75
diff -b -B -U 4 -r1.74 -r1.75
--- src/lib/net.c	19 Oct 2009 21:15:07 -0000	1.74
+++ src/lib/net.c	20 Oct 2009 22:39:47 -0000	1.75
@@ -815,26 +815,28 @@
 
 return pipefd[0];
 }
 
-int netHttpConnect(char *url, char *method, char *protocol, char *agent)
+
+int netHttpConnect(char *url, char *method, char *protocol, char *agent, char *optionalHeader)
 /* Parse URL, connect to associated server on port,
  * and send most of the request to the server.  If
  * specified in the url send user name and password
- * too.  This does not send the final \r\n to finish
- * off the request, so that you can send cookies. 
- * Typically the "method" will be "GET" or "POST"
+ * too.  Typically the "method" will be "GET" or "POST"
  * and the agent will be the name of your program or
- * library. */
+ * library. optionalHeader may be NULL or contain
+ * additional header lines such as cookie info. */
 {
 struct netParsedUrl npu;
 struct dyString *dy = newDyString(512);
 int sd;
 
 /* Parse the URL and connect. */
 netParseUrl(url, &npu);
 if (sameString(npu.protocol, "http"))
+    {
     sd = netMustConnect(npu.host, atoi(npu.port));
+    }
 else if (sameString(npu.protocol, "https"))
     {
     sd = netMustConnectHttps(npu.host, atoi(npu.port));
     }
@@ -867,8 +869,15 @@
     dyStringPrintf(dy, "Range: bytes=%lld-%lld\r\n"
 	, (long long) npu.byteRangeStart
 	, (long long) npu.byteRangeEnd);
     }
+
+if (optionalHeader)
+    dyStringAppend(dy, optionalHeader);
+
+/* finish off the header with final blank line */
+dyStringAppend(dy, "\r\n");
+
 mustWriteFd(sd, dy->string, dy->stringSize);
 
 /* Clean up and return handle. */
 dyStringFree(&dy);
@@ -876,31 +885,27 @@
 }
 
 
 
-int netOpenHttpExt(char *url, char *method, boolean end)
-/* Return a file handle that will read the url.  If end is not
- * set then can send cookies and other info to returned file 
- * handle before reading. */
-{
-int sd =  netHttpConnect(url, method, "HTTP/1.0", "genome.ucsc.edu/net.c");
-if (end)
-    mustWriteFd(sd, "\r\n", 2);
-return sd;
+int netOpenHttpExt(char *url, char *method, char *optionalHeader)
+/* Return a file handle that will read the url.  optionalHeader
+ * may by NULL or may contain cookies and other info.  */
+{
+return netHttpConnect(url, method, "HTTP/1.0", "genome.ucsc.edu/net.c", optionalHeader);
 }
 
 static int netGetOpenHttp(char *url)
 /* Return a file handle that will read the url.  */
 {
-return netOpenHttpExt(url, "GET", TRUE);
+return netOpenHttpExt(url, "GET", NULL);
 }
 
 int netUrlHead(char *url, struct hash *hash)
 /* Go get head and return status.  Return negative number if
  * can't get head. If hash is non-null, fill it with header
  * lines, including hopefully Content-Type: */
 {
-int sd = netOpenHttpExt(url, "HEAD", TRUE);
+int sd = netOpenHttpExt(url, "HEAD", NULL);
 int status = EIO;
 if (sd >= 0)
     {
     char *line, *word;