forward-port r4a30bae41553 from stable
authorArt Cancro <ajc@citadel.org>
Mon, 3 Jan 2011 17:08:35 +0000 (12:08 -0500)
committerWilfried Goesgens <dothebart@citadel.org>
Sun, 4 Sep 2011 11:16:55 +0000 (11:16 +0000)
citadel/file_ops.c
webcit/tcp_sockets.c
webcit/webserver.c

index ee2e93a05f2ddae13451f3ca52bad8746b328114..20bc03b68af2f900e6693061238a18f4901c596e 100644 (file)
@@ -641,7 +641,6 @@ void cmd_ucls(char *cmd)
 }
 
 
-
 /*
  * read from the download file
  */
@@ -649,9 +648,9 @@ void cmd_read(char *cmdbuf)
 {
        long start_pos;
        size_t bytes;
-       size_t actual_bytes;
-       char *buf = NULL;
+       char buf[SIZ];
 
+       /* The client will transmit its requested offset and byte count */
        start_pos = extract_long(cmdbuf, 0);
        bytes = extract_int(cmdbuf, 1);
 
@@ -661,26 +660,24 @@ void cmd_read(char *cmdbuf)
                return;
        }
 
-       buf = mmap(NULL, 
-                  CC->download_fp_total, 
-                  PROT_READ, 
-                  MAP_PRIVATE,
-                  fileno(CC->download_fp), 
-                  0);
-       
-       actual_bytes = CC->download_fp_total - start_pos;
-       if ((actual_bytes > 0) && (buf != NULL)) {
-               cprintf("%d %d\n", BINARY_FOLLOWS, (int)actual_bytes);
-               client_write(buf + start_pos, actual_bytes);
+       /* If necessary, reduce the byte count to the size of our buffer */
+       if (bytes > sizeof(buf)) {
+               bytes = sizeof(buf);
+       }
+
+       fseek(CC->download_fp, start_pos, 0);
+       bytes = fread(buf, 1, bytes, CC->download_fp);
+       if (bytes > 0) {
+               /* Tell the client the actual byte count and transmit it */
+               cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
+               client_write(bufbytes);
        }
        else {
                cprintf("%d %s\n", ERROR, strerror(errno));
        }
-       munmap(buf, CC->download_fp_total);
 }
 
 
-
 /*
  * write to the upload file
  */
index 2ee3cb27e0b81e0289c766cdc70eadf97d1797a3..696dfc3ae76dd73357765e98aca5cbbc9dfadaee 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1987-2010 by the citadel.org team
+ * Copyright (c) 1987-2011 by the citadel.org team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -381,116 +381,45 @@ void serv_printf(const char *format,...)
 }
 
 
-
-/**
- * Read binary data from server into memory using a series of
- * server READ commands.
- * \return the read content as StrBuf
+/*
+ * Read binary data from server into memory using a series of server READ commands.
+ * returns the read content as StrBuf
  */
 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf) 
 {
        wcsession *WCC = WC;
-       size_t bytes = 0;
-       size_t thisblock = 0;
-       
-       if (Ret == NULL)
-           return -1;
+       size_t bytes_read = 0;
+       size_t this_block = 0;
+       int rc;
 
-       if (MaxRead == -1)
-       {
-               serv_printf("READ %d|"SIZE_T_FMT, 0, total_len);
-               if (StrBuf_ServGetln(Buf) > 0)
-               {
-                       long YetRead;
-                       const char *ErrStr;
-                       const char *pch;
-                       int rc;
-
-                       if (GetServerStatus(Buf, NULL) == 6)
-                       {
-                           StrBufCutLeft(Buf, 4);
-                           thisblock = StrTol(Buf);
-                           if (WCC->serv_sock==-1) {
-                                   FlushStrBuf(Ret); 
-                                   return -1; 
-                           }
-
-                           if (WCC->ReadPos != NULL) {
-                                   pch = ChrPtr(WCC->ReadBuf);
-
-                                   YetRead = WCC->ReadPos - pch;
-                                   if (YetRead > 0)
-                                   {
-                                           long StillThere;
-                                           
-                                           StillThere = StrLength(WCC->ReadBuf) - 
-                                                   YetRead;
-                                           
-                                           StrBufPlain(Ret, 
-                                                       WCC->ReadPos,
-                                                       StillThere);
-                                           total_len -= StillThere;
-                                   }
-                                   FlushStrBuf(WCC->ReadBuf);
-                                   WCC->ReadPos = NULL;
-                           } 
-                           if (total_len > 0)
-                           {
-                                   rc = StrBufReadBLOB(Ret, 
-                                                       &WCC->serv_sock, 
-                                                       1, 
-                                                       total_len,
-                                                       &ErrStr);
-                                   if (rc < 0)
-                                   {
-                                           lprintf(1, "Server connection broken: %s\n",
-                                                   (ErrStr)?ErrStr:"");
-                                           wc_backtrace();
-                                           WCC->serv_sock = (-1);
-                                           WCC->connected = 0;
-                                           WCC->logged_in = 0;
-                                           return rc;
-                                   }
-                                   else
-                                           return StrLength(Ret);
-                           }
-                           else 
-                                   return StrLength(Ret);
-                       }
-               }
-               else
-                       return -1;
+       if (Ret == NULL) {
+               return -1;
        }
-       else while ((WCC->serv_sock!=-1) &&
-              (bytes < total_len)) {
-               thisblock = MaxRead;
-               if ((total_len - bytes) < thisblock) {
-                       thisblock = total_len - bytes;
-                       if (thisblock == 0) {
-                               FlushStrBuf(Ret); 
-                               return -1; 
-                       }
+
+       while (bytes_read < total_len) {
+
+               if (WCC->serv_sock==-1) {
+                       FlushStrBuf(Ret); 
+                       return -1; 
                }
-               serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
-               if (StrBuf_ServGetln(Buf) > 0)
-               {
-                       if (GetServerStatus(Buf, NULL) == 6)
-                       {
-                           StrBufCutLeft(Buf, 4);
-                           thisblock = StrTol(Buf);
-                           if (WCC->serv_sock==-1) {
-                                   FlushStrBuf(Ret); 
-                                   return -1; 
-                           }
-                           StrBuf_ServGetBLOBBuffered(Ret, thisblock);
-                           bytes += thisblock;
-                   }
-                   else {
-                           lprintf(3, "Error: %s\n", ChrPtr(Buf) + 4);
-                           return -1;
-                   }
+
+               serv_printf("READ %d|%d", bytes_read, total_len-bytes_read);
+               if ( (StrBuf_ServGetln(Buf) > 0) && (GetServerStatus(Buf, NULL) == 6) ) {
+                       StrBufCutLeft(Buf, 4);
+                       this_block = StrTol(Buf);
+                       rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
+                       if (rc < 0) {
+                               lprintf(1, "Server connection broken during download\n");
+                               wc_backtrace();
+                               WCC->serv_sock = (-1);
+                               WCC->connected = 0;
+                               WCC->logged_in = 0;
+                               return rc;
+                       }
+                       bytes_read += rc;
                }
        }
+
        return StrLength(Ret);
 }
 
index 87b83afad5b71d5a5e9987f265e28c6e47d3fb01..858f0b61e0aee2332196023cad3b619174fae366 100644 (file)
@@ -20,7 +20,6 @@ int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
 extern int msock;                      /* master listening socket */
 extern int verbosity;          /* Logging level */
 extern char static_icon_dir[PATH_MAX];          /* where should we find our mime icons */
-extern long MaxRead; 
 int is_https = 0;              /* Nonzero if I am an HTTPS service */
 int follow_xff = 0;            /* Follow X-Forwarded-For: header */
 int home_specified = 0;                /* did the user specify a homedir? */
@@ -113,9 +112,9 @@ int main(int argc, char **argv)
 
        /* Parse command line */
 #ifdef HAVE_OPENSSL
-       while ((a = getopt(argc, argv, "R:u:h:i:p:t:T:B:x:dD:G:cfsS:Z")) != EOF)
+       while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:cfsS:Z")) != EOF)
 #else
-       while ((a = getopt(argc, argv, "R:u:h:i:p:t:T:B:x:dD:G:cfZ")) != EOF)
+       while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:cfZ")) != EOF)
 #endif
                switch (a) {
                case 'u':
@@ -204,9 +203,6 @@ int main(int argc, char **argv)
                        I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
                        I18nDumpFile = optarg;
                        break;
-               case 'R':
-                       MaxRead = atol(optarg);
-                       break;
                default:
                        fprintf(stderr, "usage: webcit "
                                "[-i ip_addr] [-p http_port] "