@matheusbsbs / matheus-bagliano / commits / acf59575ca
Fork 0

Improve XML parsing in http-push

Improved XML parsing - replace specialized doc parser callbacks with generic functions that track the parser context and use document-specific callbacks to process that data. Signed-off-by: Nick Hengeveld <nickh@reactrix.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nick Hengeveld committed Nov 18, 2005 at 11:03 UTC acf59575cac330f3a505247dd7a9d6551a2a9cf8
1 file changed +163 -156
http-push.c
+163 -156
@@ -24,13 +24,28 @@ enum XML_Status {
24
25 #define RANGE_HEADER_SIZE 30
26
27 -/* DAV method names and request body templates */
27 +/* DAV methods */
28 #define DAV_LOCK "LOCK"
29 #define DAV_MKCOL "MKCOL"
30 #define DAV_MOVE "MOVE"
31 #define DAV_PROPFIND "PROPFIND"
32 #define DAV_PUT "PUT"
33 #define DAV_UNLOCK "UNLOCK"
34 +
35 +/* DAV lock flags */
36 +#define DAV_PROP_LOCKWR (1u << 0)
37 +#define DAV_PROP_LOCKEX (1u << 1)
38 +#define DAV_LOCK_OK (1u << 2)
39 +
40 +/* DAV XML properties */
41 +#define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
42 +#define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
43 +#define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
44 +#define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
45 +#define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
46 +#define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
47 +
48 +/* DAV request body templates */
49 #define PROPFIND_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
50 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
51
@@ -92,14 +107,17 @@ struct transfer_request
107
108 static struct transfer_request *request_queue_head = NULL;
109
110 +struct xml_ctx
111 +{
112 + char *name;
113 + int len;
114 + char *cdata;
115 + void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
116 + void *userData;
117 +};
118 +
119 struct active_lock
120 {
97 - int ctx_activelock;
98 - int ctx_owner;
99 - int ctx_owner_href;
100 - int ctx_timeout;
101 - int ctx_locktoken;
102 - int ctx_locktoken_href;
121 char *url;
122 char *owner;
123 char *token;
@@ -108,16 +126,6 @@ struct active_lock
126 int refreshing;
127 };
128
111 -struct lockprop
112 -{
113 - int supported_lock;
114 - int lock_entry;
115 - int lock_scope;
116 - int lock_type;
117 - int lock_exclusive;
118 - int lock_exclusive_write;
119 -};
120 -
129 static void finish_request(struct transfer_request *request);
130
131 static void process_response(void *callback_data)
@@ -734,107 +742,101 @@ int fetch_ref(char *ref, unsigned char *sha1)
742 return 0;
743 }
744
737 -static void
738 -start_activelock_element(void *userData, const char *name, const char **atts)
745 +static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
746 {
740 - struct active_lock *lock = (struct active_lock *)userData;
741 -
742 - if (lock->ctx_activelock && !strcmp(name, "D:timeout"))
743 - lock->ctx_timeout = 1;
744 - else if (lock->ctx_owner && strstr(name, "href"))
745 - lock->ctx_owner_href = 1;
746 - else if (lock->ctx_activelock && strstr(name, "owner"))
747 - lock->ctx_owner = 1;
748 - else if (lock->ctx_locktoken && !strcmp(name, "D:href"))
749 - lock->ctx_locktoken_href = 1;
750 - else if (lock->ctx_activelock && !strcmp(name, "D:locktoken"))
751 - lock->ctx_locktoken = 1;
752 - else if (!strcmp(name, "D:activelock"))
753 - lock->ctx_activelock = 1;
747 + int *lock_flags = (int *)ctx->userData;
748 +
749 + if (tag_closed) {
750 + if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
751 + if ((*lock_flags & DAV_PROP_LOCKEX) &&
752 + (*lock_flags & DAV_PROP_LOCKWR)) {
753 + *lock_flags |= DAV_LOCK_OK;
754 + }
755 + *lock_flags &= DAV_LOCK_OK;
756 + } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
757 + *lock_flags |= DAV_PROP_LOCKWR;
758 + } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
759 + *lock_flags |= DAV_PROP_LOCKEX;
760 + }
761 + }
762 }
763
756 -static void
757 -end_activelock_element(void *userData, const char *name)
764 +static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
765 {
759 - struct active_lock *lock = (struct active_lock *)userData;
760 -
761 - if (lock->ctx_timeout && !strcmp(name, "D:timeout")) {
762 - lock->ctx_timeout = 0;
763 - } else if (lock->ctx_owner_href && strstr(name, "href")) {
764 - lock->ctx_owner_href = 0;
765 - } else if (lock->ctx_owner && strstr(name, "owner")) {
766 - lock->ctx_owner = 0;
767 - } else if (lock->ctx_locktoken_href && !strcmp(name, "D:href")) {
768 - lock->ctx_locktoken_href = 0;
769 - } else if (lock->ctx_locktoken && !strcmp(name, "D:locktoken")) {
770 - lock->ctx_locktoken = 0;
771 - } else if (lock->ctx_activelock && !strcmp(name, "D:activelock")) {
772 - lock->ctx_activelock = 0;
766 + struct active_lock *lock = (struct active_lock *)ctx->userData;
767 +
768 + if (tag_closed && ctx->cdata) {
769 + if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
770 + lock->owner = xmalloc(strlen(ctx->cdata) + 1);
771 + strcpy(lock->owner, ctx->cdata);
772 + } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
773 + if (!strncmp(ctx->cdata, "Second-", 7))
774 + lock->timeout =
775 + strtol(ctx->cdata + 7, NULL, 10);
776 + } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
777 + if (!strncmp(ctx->cdata, "opaquelocktoken:", 16)) {
778 + lock->token = xmalloc(strlen(ctx->cdata - 15));
779 + strcpy(lock->token, ctx->cdata + 16);
780 + }
781 + }
782 }
783 }
784
785 static void
777 -activelock_cdata(void *userData, const XML_Char *s, int len)
786 +xml_start_tag(void *userData, const char *name, const char **atts)
787 {
779 - struct active_lock *lock = (struct active_lock *)userData;
780 - char *this = malloc(len+1);
781 - strncpy(this, s, len);
782 -
783 - if (lock->ctx_owner_href) {
784 - lock->owner = malloc(len+1);
785 - strcpy(lock->owner, this);
786 - } else if (lock->ctx_locktoken_href) {
787 - if (!strncmp(this, "opaquelocktoken:", 16)) {
788 - lock->token = malloc(len-15);
789 - strcpy(lock->token, this+16);
790 - }
791 - } else if (lock->ctx_timeout) {
792 - if (!strncmp(this, "Second-", 7))
793 - lock->timeout = strtol(this+7, NULL, 10);
788 + struct xml_ctx *ctx = (struct xml_ctx *)userData;
789 + const char *c = index(name, ':');
790 + int new_len;
791 +
792 + if (c == NULL)
793 + c = name;
794 + else
795 + c++;
796 +
797 + new_len = strlen(ctx->name) + strlen(c) + 2;
798 +
799 + if (new_len > ctx->len) {
800 + ctx->name = xrealloc(ctx->name, new_len);
801 + ctx->len = new_len;
802 }
803 + strcat(ctx->name, ".");
804 + strcat(ctx->name, c);
805
796 - free(this);
806 + if (ctx->cdata) {
807 + free(ctx->cdata);
808 + ctx->cdata = NULL;
809 + }
810 +
811 + ctx->userFunc(ctx, 0);
812 }
813
814 static void
800 -start_lockprop_element(void *userData, const char *name, const char **atts)
815 +xml_end_tag(void *userData, const char *name)
816 {
802 - struct lockprop *prop = (struct lockprop *)userData;
817 + struct xml_ctx *ctx = (struct xml_ctx *)userData;
818 + const char *c = index(name, ':');
819 + char *ep;
820
804 - if (prop->lock_type && !strcmp(name, "D:write")) {
805 - if (prop->lock_exclusive) {
806 - prop->lock_exclusive_write = 1;
807 - }
808 - } else if (prop->lock_scope && !strcmp(name, "D:exclusive")) {
809 - prop->lock_exclusive = 1;
810 - } else if (prop->lock_entry) {
811 - if (!strcmp(name, "D:lockscope")) {
812 - prop->lock_scope = 1;
813 - } else if (!strcmp(name, "D:locktype")) {
814 - prop->lock_type = 1;
815 - }
816 - } else if (prop->supported_lock) {
817 - if (!strcmp(name, "D:lockentry")) {
818 - prop->lock_entry = 1;
819 - }
820 - } else if (!strcmp(name, "D:supportedlock")) {
821 - prop->supported_lock = 1;
822 - }
821 + ctx->userFunc(ctx, 1);
822 +
823 + if (c == NULL)
824 + c = name;
825 + else
826 + c++;
827 +
828 + ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
829 + *ep = 0;
830 }
831
832 static void
826 -end_lockprop_element(void *userData, const char *name)
833 +xml_cdata(void *userData, const XML_Char *s, int len)
834 {
828 - struct lockprop *prop = (struct lockprop *)userData;
829 -
830 - if (!strcmp(name, "D:lockentry")) {
831 - prop->lock_entry = 0;
832 - prop->lock_scope = 0;
833 - prop->lock_type = 0;
834 - prop->lock_exclusive = 0;
835 - } else if (!strcmp(name, "D:supportedlock")) {
836 - prop->supported_lock = 0;
837 - }
835 + struct xml_ctx *ctx = (struct xml_ctx *)userData;
836 + if (ctx->cdata)
837 + free(ctx->cdata);
838 + ctx->cdata = xcalloc(len+1, 1);
839 + strncpy(ctx->cdata, s, len);
840 }
841
842 static struct active_lock *lock_remote(char *file, long timeout)
@@ -847,10 +849,11 @@ static struct active_lock *lock_remote(char *file, long timeout)
849 char *url;
850 char *ep;
851 char timeout_header[25];
850 - struct active_lock *new_lock;
852 + struct active_lock *new_lock = NULL;
853 XML_Parser parser = XML_ParserCreate(NULL);
854 enum XML_Status result;
855 struct curl_slist *dav_headers = NULL;
856 + struct xml_ctx ctx;
857
858 url = xmalloc(strlen(remote->url) + strlen(file) + 1);
859 sprintf(url, "%s%s", remote->url, file);
@@ -894,12 +897,6 @@ static struct active_lock *lock_remote(char *file, long timeout)
897 in_buffer.posn = 0;
898 in_buffer.buffer = in_data;
899
897 - new_lock = xcalloc(1, sizeof(*new_lock));
898 - new_lock->owner = NULL;
899 - new_lock->token = NULL;
900 - new_lock->timeout = -1;
901 - new_lock->refreshing = 0;
902 -
900 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
901 dav_headers = curl_slist_append(dav_headers, timeout_header);
902 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
@@ -915,40 +912,41 @@ static struct active_lock *lock_remote(char *file, long timeout)
912 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
913 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
914
915 + new_lock = xcalloc(1, sizeof(*new_lock));
916 + new_lock->owner = NULL;
917 + new_lock->token = NULL;
918 + new_lock->timeout = -1;
919 + new_lock->refreshing = 0;
920 +
921 if (start_active_slot(slot)) {
922 run_active_slot(slot);
920 - if (slot->curl_result != CURLE_OK) {
921 - fprintf(stderr, "Got HTTP error %ld\n", slot->http_code);
922 - free(new_lock);
923 - free(url);
924 - free(out_data);
925 - free(in_data);
926 - return NULL;
923 + if (slot->curl_result == CURLE_OK) {
924 + ctx.name = xcalloc(10, 1);
925 + ctx.len = 0;
926 + ctx.cdata = NULL;
927 + ctx.userFunc = handle_new_lock_ctx;
928 + ctx.userData = new_lock;
929 + XML_SetUserData(parser, &ctx);
930 + XML_SetElementHandler(parser, xml_start_tag,
931 + xml_end_tag);
932 + XML_SetCharacterDataHandler(parser, xml_cdata);
933 + result = XML_Parse(parser, in_buffer.buffer,
934 + in_buffer.posn, 1);
935 + free(ctx.name);
936 + if (result != XML_STATUS_OK) {
937 + fprintf(stderr, "XML error: %s\n",
938 + XML_ErrorString(
939 + XML_GetErrorCode(parser)));
940 + new_lock->timeout = -1;
941 + }
942 }
943 } else {
929 - free(new_lock);
930 - free(url);
931 - free(out_data);
932 - free(in_data);
944 fprintf(stderr, "Unable to start request\n");
934 - return NULL;
945 }
946
947 + curl_slist_free_all(dav_headers);
948 free(out_data);
938 -
939 - XML_SetUserData(parser, new_lock);
940 - XML_SetElementHandler(parser, start_activelock_element,
941 - end_activelock_element);
942 - XML_SetCharacterDataHandler(parser, activelock_cdata);
943 - result = XML_Parse(parser, in_buffer.buffer, in_buffer.posn, 1);
949 free(in_data);
945 - if (result != XML_STATUS_OK) {
946 - fprintf(stderr, "%s", XML_ErrorString(
947 - XML_GetErrorCode(parser)));
948 - free(url);
949 - free(new_lock);
950 - return NULL;
951 - }
950
951 if (new_lock->token == NULL || new_lock->timeout <= 0) {
952 if (new_lock->token != NULL)
@@ -957,11 +955,12 @@ static struct active_lock *lock_remote(char *file, long timeout)
955 free(new_lock->owner);
956 free(url);
957 free(new_lock);
960 - return NULL;
958 + new_lock = NULL;
959 + } else {
960 + new_lock->url = url;
961 + new_lock->start_time = time(NULL);
962 }
963
963 - new_lock->url = url;
964 - new_lock->start_time = time(NULL);
964 return new_lock;
965 }
966
@@ -1000,13 +999,15 @@ static int unlock_remote(struct active_lock *lock)
999 if (lock->owner != NULL)
1000 free(lock->owner);
1001 free(lock->url);
1002 +/* Freeing the token causes a segfault...
1003 free(lock->token);
1004 +*/
1005 free(lock);
1006
1007 return rc;
1008 }
1009
1009 -static int check_locking(void)
1010 +static int locking_available(void)
1011 {
1012 struct active_request_slot *slot;
1013 struct buffer in_buffer;
@@ -1015,8 +1016,9 @@ static int check_locking(void)
1016 char *out_data;
1017 XML_Parser parser = XML_ParserCreate(NULL);
1018 enum XML_Status result;
1018 - struct lockprop supported_lock;
1019 struct curl_slist *dav_headers = NULL;
1020 + struct xml_ctx ctx;
1021 + int lock_flags = 0;
1022
1023 out_buffer.size = strlen(PROPFIND_REQUEST) + strlen(remote->url) - 2;
1024 out_data = xmalloc(out_buffer.size + 1);
@@ -1045,30 +1047,35 @@ static int check_locking(void)
1047
1048 if (start_active_slot(slot)) {
1049 run_active_slot(slot);
1048 - free(out_data);
1049 - if (slot->curl_result != CURLE_OK) {
1050 - free(in_buffer.buffer);
1051 - return -1;
1050 + if (slot->curl_result == CURLE_OK) {
1051 + ctx.name = xcalloc(10, 1);
1052 + ctx.len = 0;
1053 + ctx.cdata = NULL;
1054 + ctx.userFunc = handle_lockprop_ctx;
1055 + ctx.userData = &lock_flags;
1056 + XML_SetUserData(parser, &ctx);
1057 + XML_SetElementHandler(parser, xml_start_tag,
1058 + xml_end_tag);
1059 + result = XML_Parse(parser, in_buffer.buffer,
1060 + in_buffer.posn, 1);
1061 + free(ctx.name);
1062 +
1063 + if (result != XML_STATUS_OK) {
1064 + fprintf(stderr, "XML error: %s\n",
1065 + XML_ErrorString(
1066 + XML_GetErrorCode(parser)));
1067 + lock_flags = 0;
1068 + }
1069 }
1053 -
1054 - XML_SetUserData(parser, &supported_lock);
1055 - XML_SetElementHandler(parser, start_lockprop_element,
1056 - end_lockprop_element);
1057 - result = XML_Parse(parser, in_buffer.buffer, in_buffer.posn, 1);
1058 - free(in_buffer.buffer);
1059 - if (result != XML_STATUS_OK)
1060 - return error("%s", XML_ErrorString(
1061 - XML_GetErrorCode(parser)));
1070 } else {
1063 - free(out_data);
1064 - free(in_buffer.buffer);
1065 - return error("Unable to start request");
1071 + fprintf(stderr, "Unable to start request\n");
1072 }
1073
1068 - if (supported_lock.lock_exclusive_write)
1069 - return 0;
1070 - else
1071 - return 1;
1074 + free(out_data);
1075 + free(in_buffer.buffer);
1076 + curl_slist_free_all(dav_headers);
1077 +
1078 + return lock_flags;
1079 }
1080
1081 static int is_ancestor(unsigned char *sha1, struct commit *commit)
@@ -1269,7 +1276,7 @@ int main(int argc, char **argv)
1276 "Pragma: no-cache");
1277
1278 /* Verify DAV compliance/lock support */
1272 - if (check_locking() != 0) {
1279 + if (!locking_available()) {
1280 fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url);
1281 rc = 1;
1282 goto cleanup;