4
#include "fetch.h"
5
#include "http.h"
6
7
-#ifndef NO_EXPAT
8
-#include <expat.h>
9
-
10
-/* Definitions for DAV requests */
11
-#define DAV_PROPFIND "PROPFIND"
12
-#define DAV_PROPFIND_RESP ".multistatus.response"
13
-#define DAV_PROPFIND_NAME ".multistatus.response.href"
14
-#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
15
-#define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
16
-
17
-/* Definitions for processing XML DAV responses */
18
-#ifndef XML_STATUS_OK
19
-enum XML_Status {
20
- XML_STATUS_OK = 1,
21
- XML_STATUS_ERROR = 0
22
-};
23
-#define XML_STATUS_OK 1
24
-#define XML_STATUS_ERROR 0
25
-#endif
26
-
27
-/* Flags that control remote_ls processing */
28
-#define PROCESS_FILES (1u << 0)
29
-#define PROCESS_DIRS (1u << 1)
30
-#define RECURSIVE (1u << 2)
31
-
32
-/* Flags that remote_ls passes to callback functions */
33
-#define IS_DIR (1u << 0)
34
-#endif
35
-
7
#define PREV_BUF_SIZE 4096
8
#define RANGE_HEADER_SIZE 30
9
61
int http_specific;
62
};
63
93
-#ifndef NO_EXPAT
94
-struct xml_ctx
95
-{
96
- char *name;
97
- int len;
98
- char *cdata;
99
- void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
100
- void *userData;
101
-};
102
-
103
-struct remote_ls_ctx
104
-{
105
- struct alt_base *repo;
106
- char *path;
107
- void (*userFunc)(struct remote_ls_ctx *ls);
108
- void *userData;
109
- int flags;
110
- char *dentry_name;
111
- int dentry_flags;
112
- int rc;
113
- struct remote_ls_ctx *parent;
114
-};
115
-#endif
116
-
64
static struct object_request *object_queue_head;
65
66
static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
661
free(url);
662
}
663
717
-#ifndef NO_EXPAT
718
-static void
719
-xml_start_tag(void *userData, const char *name, const char **atts)
720
-{
721
- struct xml_ctx *ctx = (struct xml_ctx *)userData;
722
- const char *c = strchr(name, ':');
723
- int new_len;
724
-
725
- if (c == NULL)
726
- c = name;
727
- else
728
- c++;
729
-
730
- new_len = strlen(ctx->name) + strlen(c) + 2;
731
-
732
- if (new_len > ctx->len) {
733
- ctx->name = xrealloc(ctx->name, new_len);
734
- ctx->len = new_len;
735
- }
736
- strcat(ctx->name, ".");
737
- strcat(ctx->name, c);
738
-
739
- free(ctx->cdata);
740
- ctx->cdata = NULL;
741
-
742
- ctx->userFunc(ctx, 0);
743
-}
744
-
745
-static void
746
-xml_end_tag(void *userData, const char *name)
747
-{
748
- struct xml_ctx *ctx = (struct xml_ctx *)userData;
749
- const char *c = strchr(name, ':');
750
- char *ep;
751
-
752
- ctx->userFunc(ctx, 1);
753
-
754
- if (c == NULL)
755
- c = name;
756
- else
757
- c++;
758
-
759
- ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
760
- *ep = 0;
761
-}
762
-
763
-static void
764
-xml_cdata(void *userData, const XML_Char *s, int len)
765
-{
766
- struct xml_ctx *ctx = (struct xml_ctx *)userData;
767
- free(ctx->cdata);
768
- ctx->cdata = xmalloc(len + 1);
769
- strlcpy(ctx->cdata, s, len + 1);
770
-}
771
-
772
-static int remote_ls(struct alt_base *repo, const char *path, int flags,
773
- void (*userFunc)(struct remote_ls_ctx *ls),
774
- void *userData);
775
-
776
-static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
777
-{
778
- struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
779
-
780
- if (tag_closed) {
781
- if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
782
- if (ls->dentry_flags & IS_DIR) {
783
- if (ls->flags & PROCESS_DIRS) {
784
- ls->userFunc(ls);
785
- }
786
- if (strcmp(ls->dentry_name, ls->path) &&
787
- ls->flags & RECURSIVE) {
788
- ls->rc = remote_ls(ls->repo,
789
- ls->dentry_name,
790
- ls->flags,
791
- ls->userFunc,
792
- ls->userData);
793
- }
794
- } else if (ls->flags & PROCESS_FILES) {
795
- ls->userFunc(ls);
796
- }
797
- } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
798
- ls->dentry_name = xmalloc(strlen(ctx->cdata) -
799
- ls->repo->path_len + 1);
800
- strcpy(ls->dentry_name, ctx->cdata + ls->repo->path_len);
801
- } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
802
- ls->dentry_flags |= IS_DIR;
803
- }
804
- } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
805
- free(ls->dentry_name);
806
- ls->dentry_name = NULL;
807
- ls->dentry_flags = 0;
808
- }
809
-}
810
-
811
-static int remote_ls(struct alt_base *repo, const char *path, int flags,
812
- void (*userFunc)(struct remote_ls_ctx *ls),
813
- void *userData)
814
-{
815
- char *url = xmalloc(strlen(repo->base) + strlen(path) + 1);
816
- struct active_request_slot *slot;
817
- struct slot_results results;
818
- struct buffer in_buffer;
819
- struct buffer out_buffer;
820
- char *in_data;
821
- char *out_data;
822
- XML_Parser parser = XML_ParserCreate(NULL);
823
- enum XML_Status result;
824
- struct curl_slist *dav_headers = NULL;
825
- struct xml_ctx ctx;
826
- struct remote_ls_ctx ls;
827
-
828
- ls.flags = flags;
829
- ls.repo = repo;
830
- ls.path = xstrdup(path);
831
- ls.dentry_name = NULL;
832
- ls.dentry_flags = 0;
833
- ls.userData = userData;
834
- ls.userFunc = userFunc;
835
- ls.rc = 0;
836
-
837
- sprintf(url, "%s%s", repo->base, path);
838
-
839
- out_buffer.size = strlen(PROPFIND_ALL_REQUEST);
840
- out_data = xmalloc(out_buffer.size + 1);
841
- snprintf(out_data, out_buffer.size + 1, PROPFIND_ALL_REQUEST);
842
- out_buffer.posn = 0;
843
- out_buffer.buffer = out_data;
844
-
845
- in_buffer.size = 4096;
846
- in_data = xmalloc(in_buffer.size);
847
- in_buffer.posn = 0;
848
- in_buffer.buffer = in_data;
849
-
850
- dav_headers = curl_slist_append(dav_headers, "Depth: 1");
851
- dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
852
-
853
- slot = get_active_slot();
854
- slot->results = &results;
855
- curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
856
- curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
857
- curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
858
- curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
859
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
860
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
861
- curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
862
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
863
- curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
864
-
865
- if (start_active_slot(slot)) {
866
- run_active_slot(slot);
867
- if (results.curl_result == CURLE_OK) {
868
- ctx.name = xcalloc(10, 1);
869
- ctx.len = 0;
870
- ctx.cdata = NULL;
871
- ctx.userFunc = handle_remote_ls_ctx;
872
- ctx.userData = &ls;
873
- XML_SetUserData(parser, &ctx);
874
- XML_SetElementHandler(parser, xml_start_tag,
875
- xml_end_tag);
876
- XML_SetCharacterDataHandler(parser, xml_cdata);
877
- result = XML_Parse(parser, in_buffer.buffer,
878
- in_buffer.posn, 1);
879
- free(ctx.name);
880
-
881
- if (result != XML_STATUS_OK) {
882
- ls.rc = error("XML error: %s",
883
- XML_ErrorString(
884
- XML_GetErrorCode(parser)));
885
- }
886
- } else {
887
- ls.rc = -1;
888
- }
889
- } else {
890
- ls.rc = error("Unable to start PROPFIND request");
891
- }
892
-
893
- free(ls.path);
894
- free(url);
895
- free(out_data);
896
- free(in_buffer.buffer);
897
- curl_slist_free_all(dav_headers);
898
-
899
- return ls.rc;
900
-}
901
-
902
-static void process_ls_pack(struct remote_ls_ctx *ls)
903
-{
904
- unsigned char sha1[20];
905
-
906
- if (strlen(ls->dentry_name) == 63 &&
907
- !strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
908
- has_extension(ls->dentry_name, ".pack")) {
909
- get_sha1_hex(ls->dentry_name + 18, sha1);
910
- setup_index(ls->repo, sha1);
911
- }
912
-}
913
-#endif
914
-
664
static int fetch_indices(struct alt_base *repo)
665
{
666
unsigned char sha1[20];
683
if (get_verbosely)
684
fprintf(stderr, "Getting pack list for %s\n", repo->base);
685
937
-#ifndef NO_EXPAT
938
- if (remote_ls(repo, "objects/pack/", PROCESS_FILES,
939
- process_ls_pack, NULL) == 0)
940
- return 0;
941
-#endif
942
-
686
url = xmalloc(strlen(repo->base) + 21);
687
sprintf(url, "%s/objects/info/packs", repo->base);
688