Revert "Update to 7.40.1"
[platform/upstream/curl.git] / lib / easy.c
index 619312b..160712e 100644 (file)
@@ -74,7 +74,6 @@
 #include "conncache.h"
 #include "multiif.h"
 #include "sigpipe.h"
-#include "ssh.h"
 
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>
@@ -137,9 +136,9 @@ static CURLcode win32_init(void)
 
 #ifdef USE_WINDOWS_SSPI
   {
-    CURLcode result = Curl_sspi_global_init();
-    if(result)
-      return result;
+    CURLcode err = Curl_sspi_global_init();
+    if(err != CURLE_OK)
+      return err;
   }
 #endif
 
@@ -244,7 +243,7 @@ CURLcode curl_global_init(long flags)
     }
 
   if(flags & CURL_GLOBAL_WIN32)
-    if(win32_init()) {
+    if(win32_init() != CURLE_OK) {
       DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
       return CURLE_FAILED_INIT;
     }
@@ -266,7 +265,7 @@ CURLcode curl_global_init(long flags)
   idna_init();
 #endif
 
-  if(Curl_resolver_global_init()) {
+  if(Curl_resolver_global_init() != CURLE_OK) {
     DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
     return CURLE_FAILED_INIT;
   }
@@ -294,7 +293,7 @@ CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
                               curl_free_callback f, curl_realloc_callback r,
                               curl_strdup_callback s, curl_calloc_callback c)
 {
-  CURLcode result = CURLE_OK;
+  CURLcode code = CURLE_OK;
 
   /* Invalid input, return immediately */
   if(!m || !f || !r || !s || !c)
@@ -309,8 +308,8 @@ CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
   }
 
   /* Call the actual init function first */
-  result = curl_global_init(flags);
-  if(!result) {
+  code = curl_global_init(flags);
+  if(code == CURLE_OK) {
     Curl_cmalloc = m;
     Curl_cfree = f;
     Curl_cstrdup = s;
@@ -318,7 +317,7 @@ CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
     Curl_ccalloc = c;
   }
 
-  return result;
+  return code;
 }
 
 /**
@@ -358,13 +357,13 @@ void curl_global_cleanup(void)
  */
 CURL *curl_easy_init(void)
 {
-  CURLcode result;
+  CURLcode res;
   struct SessionHandle *data;
 
   /* Make sure we inited the global SSL stuff */
   if(!initialized) {
-    result = curl_global_init(CURL_GLOBAL_DEFAULT);
-    if(result) {
+    res = curl_global_init(CURL_GLOBAL_DEFAULT);
+    if(res) {
       /* something in the global init failed, return nothing */
       DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
       return NULL;
@@ -372,8 +371,8 @@ CURL *curl_easy_init(void)
   }
 
   /* We use curl_open() with undefined URL so far */
-  result = Curl_open(&data);
-  if(result) {
+  res = Curl_open(&data);
+  if(res != CURLE_OK) {
     DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
     return NULL;
   }
@@ -391,17 +390,17 @@ CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
 {
   va_list arg;
   struct SessionHandle *data = curl;
-  CURLcode result;
+  CURLcode ret;
 
   if(!curl)
     return CURLE_BAD_FUNCTION_ARGUMENT;
 
   va_start(arg, tag);
 
-  result = Curl_setopt(data, tag, arg);
+  ret = Curl_setopt(data, tag, arg);
 
   va_end(arg);
-  return result;
+  return ret;
 }
 
 #ifdef CURLDEBUG
@@ -491,10 +490,6 @@ static int events_socket(CURL *easy,      /* easy handle */
   struct events *ev = userp;
   struct socketmonitor *m;
   struct socketmonitor *prev=NULL;
-
-#if defined(CURL_DISABLE_VERBOSE_STRINGS)
-  (void) easy;
-#endif
   (void)socketp;
 
   m = ev->list;
@@ -575,7 +570,7 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
 {
   bool done = FALSE;
   CURLMcode mcode;
-  CURLcode result = CURLE_OK;
+  CURLcode rc = CURLE_OK;
 
   while(!done) {
     CURLMsg *msg;
@@ -635,9 +630,6 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
         ev->ms += curlx_tvdiff(after, before);
 
     }
-    else
-      return CURLE_RECV_ERROR;
-
     if(mcode)
       return CURLE_URL_MALFORMAT; /* TODO: return a proper error! */
 
@@ -645,12 +637,12 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
        second argument */
     msg = curl_multi_info_read(multi, &pollrc);
     if(msg) {
-      result = msg->data.result;
+      rc = msg->data.result;
       done = TRUE;
     }
   }
 
-  return result;
+  return rc;
 }
 
 
@@ -676,7 +668,7 @@ static CURLcode easy_transfer(CURLM *multi)
 {
   bool done = FALSE;
   CURLMcode mcode = CURLM_OK;
-  CURLcode result = CURLE_OK;
+  CURLcode code = CURLE_OK;
   struct timeval before;
   int without_fds = 0;  /* count number of consecutive returns from
                            curl_multi_wait() without any filedescriptors */
@@ -691,7 +683,7 @@ static CURLcode easy_transfer(CURLM *multi)
     if(mcode == CURLM_OK) {
       if(ret == -1) {
         /* poll() failed not on EINTR, indicate a network problem */
-        result = CURLE_RECV_ERROR;
+        code = CURLE_RECV_ERROR;
         break;
       }
       else if(ret == 0) {
@@ -722,7 +714,7 @@ static CURLcode easy_transfer(CURLM *multi)
       int rc;
       CURLMsg *msg = curl_multi_info_read(multi, &rc);
       if(msg) {
-        result = msg->data.result;
+        code = msg->data.result;
         done = TRUE;
       }
     }
@@ -736,7 +728,7 @@ static CURLcode easy_transfer(CURLM *multi)
             CURLE_BAD_FUNCTION_ARGUMENT;
   }
 
-  return result;
+  return code;
 }
 
 
@@ -761,7 +753,7 @@ static CURLcode easy_perform(struct SessionHandle *data, bool events)
 {
   CURLM *multi;
   CURLMcode mcode;
-  CURLcode result = CURLE_OK;
+  CURLcode code = CURLE_OK;
   SIGPIPE_VARIABLE(pipe_st);
 
   if(!data)
@@ -802,7 +794,7 @@ static CURLcode easy_perform(struct SessionHandle *data, bool events)
   data->multi = multi;
 
   /* run the transfer */
-  result = events ? easy_events(multi) : easy_transfer(multi);
+  code = events ? easy_events(multi) : easy_transfer(multi);
 
   /* ignoring the return code isn't nice, but atm we can't really handle
      a failure here, room for future improvement! */
@@ -811,7 +803,7 @@ static CURLcode easy_perform(struct SessionHandle *data, bool events)
   sigpipe_restore(&pipe_st);
 
   /* The multi handle is kept alive, owned by the easy handle */
-  return result;
+  return code;
 }
 
 
@@ -862,16 +854,16 @@ CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
 {
   va_list arg;
   void *paramp;
-  CURLcode result;
+  CURLcode ret;
   struct SessionHandle *data = (struct SessionHandle *)curl;
 
   va_start(arg, info);
   paramp = va_arg(arg, void *);
 
-  result = Curl_getinfo(data, info, paramp);
+  ret = Curl_getinfo(data, info, paramp);
 
   va_end(arg);
-  return result;
+  return ret;
 }
 
 /*
@@ -898,7 +890,7 @@ CURL *curl_easy_duphandle(CURL *incurl)
   outcurl->state.headersize = HEADERSIZE;
 
   /* copy all userdefined values */
-  if(Curl_dupset(outcurl, data))
+  if(Curl_dupset(outcurl, data) != CURLE_OK)
     goto fail;
 
   /* the connection cache is setup on demand */
@@ -944,7 +936,7 @@ CURL *curl_easy_duphandle(CURL *incurl)
 
   /* Clone the resolver handle, if present, for the new handle */
   if(Curl_resolver_duphandle(&outcurl->state.resolver,
-                             data->state.resolver))
+                             data->state.resolver) != CURLE_OK)
     goto fail;
 
   Curl_convert_setup(outcurl);
@@ -1026,15 +1018,73 @@ CURLcode curl_easy_pause(CURL *curl, int action)
     /* we have a buffer for sending that we now seem to be able to deliver
        since the receive pausing is lifted! */
 
-    /* get the pointer in local copy since the function may return PAUSE
-       again and then we'll get a new copy allocted and stored in
+    /* get the pointer, type and length in local copies since the function may
+       return PAUSE again and then we'll get a new copy allocted and stored in
        the tempwrite variables */
     char *tempwrite = data->state.tempwrite;
-
+    char *freewrite = tempwrite; /* store this pointer to free it later */
+    size_t tempsize = data->state.tempwritesize;
+    int temptype = data->state.tempwritetype;
+    size_t chunklen;
+
+    /* clear tempwrite here just to make sure it gets cleared if there's no
+       further use of it, and make sure we don't clear it after the function
+       invoke as it may have been set to a new value by then */
     data->state.tempwrite = NULL;
-    result = Curl_client_chop_write(data->easy_conn, data->state.tempwritetype,
-                                    tempwrite, data->state.tempwritesize);
-    free(tempwrite);
+
+    /* since the write callback API is define to never exceed
+       CURL_MAX_WRITE_SIZE bytes in a single call, and since we may in fact
+       have more data than that in our buffer here, we must loop sending the
+       data in multiple calls until there's no data left or we get another
+       pause returned.
+
+       A tricky part is that the function we call will "buffer" the data
+       itself when it pauses on a particular buffer, so we may need to do some
+       extra trickery if we get a pause return here.
+    */
+    do {
+      chunklen = (tempsize > CURL_MAX_WRITE_SIZE)?CURL_MAX_WRITE_SIZE:tempsize;
+
+      result = Curl_client_write(data->easy_conn,
+                                 temptype, tempwrite, chunklen);
+      if(result)
+        /* failures abort the loop at once */
+        break;
+
+      if(data->state.tempwrite && (tempsize - chunklen)) {
+        /* Ouch, the reading is again paused and the block we send is now
+           "cached". If this is the final chunk we can leave it like this, but
+           if we have more chunks that are cached after this, we need to free
+           the newly cached one and put back a version that is truly the entire
+           contents that is saved for later
+        */
+        char *newptr;
+
+        /* note that tempsize is still the size as before the callback was
+           used, and thus the whole piece of data to keep */
+        newptr = realloc(data->state.tempwrite, tempsize);
+
+        if(!newptr) {
+          free(data->state.tempwrite); /* free old area */
+          data->state.tempwrite = NULL;
+          result = CURLE_OUT_OF_MEMORY;
+          /* tempwrite will be freed further down */
+          break;
+        }
+        data->state.tempwrite = newptr; /* store new pointer */
+        memcpy(newptr, tempwrite, tempsize);
+        data->state.tempwritesize = tempsize; /* store new size */
+        /* tempwrite will be freed further down */
+        break; /* go back to pausing until further notice */
+      }
+      else {
+        tempsize -= chunklen;  /* left after the call above */
+        tempwrite += chunklen; /* advance the pointer */
+      }
+
+    } while((result == CURLE_OK) && tempsize);
+
+    free(freewrite); /* this is unconditionally no longer used */
   }
 
   /* if there's no error and we're not pausing both directions, we want
@@ -1079,20 +1129,20 @@ static CURLcode easy_connection(struct SessionHandle *data,
 CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n)
 {
   curl_socket_t sfd;
-  CURLcode result;
+  CURLcode ret;
   ssize_t n1;
   struct connectdata *c;
   struct SessionHandle *data = (struct SessionHandle *)curl;
 
-  result = easy_connection(data, &sfd, &c);
-  if(result)
-    return result;
+  ret = easy_connection(data, &sfd, &c);
+  if(ret)
+    return ret;
 
   *n = 0;
-  result = Curl_read(c, sfd, buffer, buflen, &n1);
+  ret = Curl_read(c, sfd, buffer, buflen, &n1);
 
-  if(result)
-    return result;
+  if(ret != CURLE_OK)
+    return ret;
 
   *n = (size_t)n1;
 
@@ -1107,26 +1157,26 @@ CURLcode curl_easy_send(CURL *curl, const void *buffer, size_t buflen,
                         size_t *n)
 {
   curl_socket_t sfd;
-  CURLcode result;
+  CURLcode ret;
   ssize_t n1;
   struct connectdata *c = NULL;
   struct SessionHandle *data = (struct SessionHandle *)curl;
 
-  result = easy_connection(data, &sfd, &c);
-  if(result)
-    return result;
+  ret = easy_connection(data, &sfd, &c);
+  if(ret)
+    return ret;
 
   *n = 0;
-  result = Curl_write(c, sfd, buffer, buflen, &n1);
+  ret = Curl_write(c, sfd, buffer, buflen, &n1);
 
   if(n1 == -1)
     return CURLE_SEND_ERROR;
 
   /* detect EAGAIN */
-  if(!result && !n1)
+  if((CURLE_OK == ret) && (0 == n1))
     return CURLE_AGAIN;
 
   *n = (size_t)n1;
 
-  return result;
+  return ret;
 }