Code

Merge branch 'sp/fix-smart-http-deadlock-on-error' into maint
authorJunio C Hamano <gitster@pobox.com>
Wed, 18 Aug 2010 23:30:11 +0000 (16:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 18 Aug 2010 23:30:11 +0000 (16:30 -0700)
* sp/fix-smart-http-deadlock-on-error:
  smart-http: Don't deadlock on server failure

1  2 
remote-curl.c

diff --combined remote-curl.c
index 24fbb9a9b972c1078b3688b2d0683c9704e09ee6,5416891c8cc3c3b51250567d2bb16df0019b7b2d..04d4813e4183c675b54aba942cd078d8ff632053
@@@ -9,7 -9,8 +9,7 @@@
  #include "sideband.h"
  
  static struct remote *remote;
 -static const char *url;
 -static struct walker *walker;
 +static const char *url; /* always ends with a trailing slash */
  
  struct options {
        int verbosity;
  };
  static struct options options;
  
 -static void init_walker(void)
 -{
 -      if (!walker)
 -              walker = get_http_walker(url, remote);
 -}
 -
  static int set_option(const char *name, const char *value)
  {
        if (!strcmp(name, "verbosity")) {
@@@ -101,7 -108,7 +101,7 @@@ static struct discovery* discover_refs(
                return last;
        free_discovery(last);
  
 -      strbuf_addf(&buffer, "%s/info/refs", url);
 +      strbuf_addf(&buffer, "%sinfo/refs", url);
        if (!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) {
                is_http = 1;
                if (!strchr(url, '?'))
        }
        refs_url = strbuf_detach(&buffer, NULL);
  
 -      init_walker();
        http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
  
        /* try again with "plain" url (no ? or & appended) */
                strbuf_reset(&buffer);
  
                proto_git_candidate = 0;
 -              strbuf_addf(&buffer, "%s/info/refs", url);
 +              strbuf_addf(&buffer, "%sinfo/refs", url);
                refs_url = strbuf_detach(&buffer, NULL);
  
                http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
        case HTTP_MISSING_TARGET:
                die("%s not found: did you run git update-server-info on the"
                    " server?", refs_url);
 +      case HTTP_NOAUTH:
 +              die("Authentication failed");
        default:
                http_error(refs_url, http_ret);
                die("HTTP request failed");
        return last;
  }
  
 -static int write_discovery(int fd, void *data)
 +static int write_discovery(int in, int out, void *data)
  {
        struct discovery *heads = data;
        int err = 0;
 -      if (write_in_full(fd, heads->buf, heads->len) != heads->len)
 +      if (write_in_full(out, heads->buf, heads->len) != heads->len)
                err = 1;
 -      close(fd);
 +      close(out);
        return err;
  }
  
@@@ -196,7 -202,6 +196,7 @@@ static struct ref *parse_git_refs(struc
        memset(&async, 0, sizeof(async));
        async.proc = write_discovery;
        async.data = heads;
 +      async.out = -1;
  
        if (start_async(&async))
                die("cannot start thread to parse advertised refs");
@@@ -244,8 -249,9 +244,8 @@@ static struct ref *parse_info_refs(stru
                i++;
        }
  
 -      init_walker();
        ref = alloc_ref("HEAD");
 -      if (!walker->fetch_ref(walker, ref) &&
 +      if (!http_fetch_ref(url, ref) &&
            !resolve_remote_symref(ref, refs)) {
                ref->next = refs;
                refs = ref;
@@@ -298,7 -304,6 +298,7 @@@ struct rpc_state 
        int out;
        struct strbuf result;
        unsigned gzip_request : 1;
 +      unsigned initial_buffer : 1;
  };
  
  static size_t rpc_out(void *ptr, size_t eltsize,
        size_t avail = rpc->len - rpc->pos;
  
        if (!avail) {
 +              rpc->initial_buffer = 0;
                avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
                if (!avail)
                        return 0;
        return avail;
  }
  
 +#ifndef NO_CURL_IOCTL
 +static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
 +{
 +      struct rpc_state *rpc = clientp;
 +
 +      switch (cmd) {
 +      case CURLIOCMD_NOP:
 +              return CURLIOE_OK;
 +
 +      case CURLIOCMD_RESTARTREAD:
 +              if (rpc->initial_buffer) {
 +                      rpc->pos = 0;
 +                      return CURLIOE_OK;
 +              }
 +              fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
 +              return CURLIOE_FAILRESTART;
 +
 +      default:
 +              return CURLIOE_UNKNOWNCMD;
 +      }
 +}
 +#endif
 +
  static size_t rpc_in(const void *ptr, size_t eltsize,
                size_t nmemb, void *buffer_)
  {
@@@ -403,13 -384,8 +403,13 @@@ static int post_rpc(struct rpc_state *r
                 */
                headers = curl_slist_append(headers, "Expect: 100-continue");
                headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
 +              rpc->initial_buffer = 1;
                curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
                curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
 +#ifndef NO_CURL_IOCTL
 +              curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
 +              curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
 +#endif
                if (options.verbosity > 1) {
                        fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
                        fflush(stderr);
@@@ -495,6 -471,7 +495,6 @@@ static int rpc_service(struct rpc_stat
        struct child_process client;
        int err = 0;
  
 -      init_walker();
        memset(&client, 0, sizeof(client));
        client.in = -1;
        client.out = -1;
        rpc->out = client.out;
        strbuf_init(&rpc->result, 0);
  
 -      strbuf_addf(&buf, "%s/%s", url, svc);
 +      strbuf_addf(&buf, "%s%s", url, svc);
        rpc->service_url = strbuf_detach(&buf, NULL);
  
        strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
                rpc->len = n;
                err |= post_rpc(rpc);
        }
-       strbuf_read(&rpc->result, client.out, 0);
  
        close(client.in);
-       close(client.out);
        client.in = -1;
+       strbuf_read(&rpc->result, client.out, 0);
+       close(client.out);
        client.out = -1;
  
        err |= finish_command(&client);
  
  static int fetch_dumb(int nr_heads, struct ref **to_fetch)
  {
 +      struct walker *walker;
        char **targets = xmalloc(nr_heads * sizeof(char*));
        int ret, i;
  
        for (i = 0; i < nr_heads; i++)
                targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
  
 -      init_walker();
 +      walker = get_http_walker(url);
        walker->get_all = 1;
        walker->get_tree = 1;
        walker->get_history = 1;
        walker->get_verbosely = options.verbosity >= 3;
        walker->get_recover = 0;
        ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
 +      walker_free(walker);
  
        for (i = 0; i < nr_heads; i++)
                free(targets[i]);
@@@ -800,15 -776,11 +801,15 @@@ int main(int argc, const char **argv
        remote = remote_get(argv[1]);
  
        if (argc > 2) {
 -              url = argv[2];
 +              end_url_with_slash(&buf, argv[2]);
        } else {
 -              url = remote->url[0];
 +              end_url_with_slash(&buf, remote->url[0]);
        }
  
 +      url = strbuf_detach(&buf, NULL);
 +
 +      http_init(remote);
 +
        do {
                if (strbuf_getline(&buf, stdin, '\n') == EOF)
                        break;
                }
                strbuf_reset(&buf);
        } while (1);
 +
 +      http_cleanup();
 +
        return 0;
  }