]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add http query string parsing and improve http header parsing
authorSeven Du <dujinfang@gmail.com>
Sat, 6 Sep 2014 07:35:10 +0000 (15:35 +0800)
committerSeven Du <dujinfang@gmail.com>
Sat, 6 Sep 2014 09:21:58 +0000 (17:21 +0800)
src/include/switch_utils.h
src/switch_utils.c

index 2c0bac818332cc911fd10e6c88583cf9af7cc563..67fee60c3cf92f24c09e75d00eb4845be45923c2 100644 (file)
@@ -24,6 +24,7 @@
  * Contributor(s):
  * 
  * Anthony Minessale II <anthm@freeswitch.org>
+ * Seven Du <dujinfang@gmail.com>
  *
  *
  * switch_utils.h -- Compatability and Helper Code
@@ -1113,6 +1114,8 @@ typedef struct switch_http_request_s {
        const char *referer;
        const char *user;
        switch_bool_t keepalive;
+       const char *content_type;
+       switch_size_t content_length;
        switch_event_t *headers;
        void *user_data;           /* private user data */
 
@@ -1134,6 +1137,16 @@ typedef struct switch_http_request_s {
 SWITCH_DECLARE(switch_status_t) switch_http_parse_header(char *buffer, uint32_t datalen, switch_http_request_t *request);
 SWITCH_DECLARE(void) switch_http_free_request(switch_http_request_t *request);
 SWITCH_DECLARE(void) switch_http_dump_request(switch_http_request_t *request);
+/**
+ * parse http query string
+ * \param[in]  request the http request object
+ * \param[in]  qs the query string buffer
+ *
+ * qs buffer will be modified, so be sure to dup the qs before passing into this function if you want to keep the original string untouched
+ * if qs is NULL, the it will parse request->qs, request->qs will be duplicated before parse to avoid being modified
+ */
+
+SWITCH_DECLARE(void) switch_http_parse_qs(switch_http_request_t *request, char *qs);
 
 SWITCH_END_EXTERN_C
 #endif
index 81fe8a192f1877e368ade2aadd1524d91a2c6a48..c08b5ff27ddfbe88c4efb5dffe61d3c4cc954f02 100644 (file)
@@ -25,6 +25,7 @@
  * 
  * Anthony Minessale II <anthm@freeswitch.org>
  * Juan Jose Comellas <juanjo@comellas.org>
+ * Seven Du <dujinfang@gmail.com>
  *
  *
  * switch_utils.c -- Compatibility and Helper Code
@@ -3603,6 +3604,47 @@ SWITCH_DECLARE(char *) switch_strerror_r(int errnum, char *buf, switch_size_t bu
 #endif
 }
 
+SWITCH_DECLARE(void) switch_http_parse_qs(switch_http_request_t *request, char *qs)
+{
+       char *q;
+       char *next;
+       char *name, *val;
+
+       if (qs) {
+               q = qs;
+       } else { /*parse our own qs, dup to avoid modify the original string */
+               q = strdup(request->qs);
+       }
+
+       switch_assert(q);
+       next = q;
+
+       do {
+               char *p;
+
+               if ((next = strchr(next, '&'))) {
+                       *next++ = '\0';
+               }
+
+               for (p = q; p && *p; p++) {
+                       if (*p == '+') *p = ' ';
+               }
+
+               switch_url_decode(q);
+
+               name = q;
+               if ((val = strchr(name, '='))) {
+                       *val++ = '\0';
+                       switch_event_add_header_string(request->headers, SWITCH_STACK_BOTTOM, name, val);
+               }
+               q = next;
+       } while (q);
+
+       if (!qs) {
+               switch_safe_free(q);
+       }
+}
+
 SWITCH_DECLARE(switch_status_t) switch_http_parse_header(char *buffer, uint32_t datalen, switch_http_request_t *request)
 {
        switch_status_t status = SWITCH_STATUS_FALSE;
@@ -3646,9 +3688,17 @@ SWITCH_DECLARE(switch_status_t) switch_http_parse_header(char *buffer, uint32_t
        p = strchr(request->uri, ' ');
 
        if (!p) goto err;
-       *p++ = '\0';
 
+       *p++ = '\0';
        http = p;
+
+       p = strchr(request->uri, '?');
+
+       if (p) {
+               *p++ = '\0';
+               request->qs = p;
+       }
+
        p = strchr(http, '\n');
 
        if (!p) goto err;
@@ -3702,9 +3752,19 @@ SWITCH_DECLARE(switch_status_t) switch_http_parse_header(char *buffer, uint32_t
 
                                if (*p) request->port = (switch_port_t)atoi(p);
                        }
+               } else if (!strncasecmp(header, "Content-Type", 12)) {
+                       request->content_type = value;
+               } else if (!strncasecmp(header, "Content-Length", 14)) {
+                       request->content_length = atoi(value);
+               } else if (!strncasecmp(header, "Referer", 7)) {
+                       request->referer = value;
                }
        }
 
+       if (request->qs) {
+               switch_http_parse_qs(request, NULL);
+       }
+
        return SWITCH_STATUS_SUCCESS;
 
 err: