introduce lws_hdr_copy_fragment
authorAndy Green <andy.green@linaro.org>
Tue, 15 Dec 2015 14:59:23 +0000 (22:59 +0800)
committerAndy Green <andy.green@linaro.org>
Tue, 15 Dec 2015 14:59:23 +0000 (22:59 +0800)
This adds a public API variant of the header copy api that lets you
choose which fragment you want copied.

Normally you want the existing one that aggregates the fragments.

But it can be useful to get each part in turn (that corresponds to
the content provided by each duplicated header normally).

Signed-off-by: Andy Green <andy.green@linaro.org>
lib/libwebsockets.h
lib/parsers.c

index bfc28e3..d667a24 100644 (file)
@@ -652,7 +652,7 @@ enum lws_token_indexes {
 };
 
 struct lws_token_limits {
-    unsigned short token_limit[WSI_TOKEN_COUNT];
+       unsigned short token_limit[WSI_TOKEN_COUNT];
 };
 
 /*
@@ -1622,9 +1622,22 @@ lws_get_library_version(void);
 LWS_VISIBLE LWS_EXTERN int
 lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
 
+/*
+ * copies the whole, aggregated header, even if it was delivered in
+ * several actual headers piece by piece
+ */
 LWS_VISIBLE LWS_EXTERN int
 lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h);
 
+/*
+ * copies only fragment frag_idx of a header.  Normally this is only useful
+ * to parse URI arguments like ?x=1&y=2, oken index WSI_TOKEN_HTTP_URI_ARGS
+ * fragment 0 will contain "x=1" and fragment 1 "y=2"
+ */
+LWS_VISIBLE LWS_EXTERN int
+lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
+                     enum lws_token_indexes h, int frag_idx);
+
 /* get the active file operations struct */
 LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops *
 lws_get_fops(struct lws_context *context);
index f20364f..c33eccc 100644 (file)
@@ -97,6 +97,29 @@ LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h)
        return len;
 }
 
+LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len,
+                                     enum lws_token_indexes h, int frag_idx)
+{
+       int n = 0;
+       int f = wsi->u.hdr.ah->frag_index[h];
+
+       while (n < frag_idx) {
+               f = wsi->u.hdr.ah->frags[f].nfrag;
+               if (!f)
+                       return -1;
+               n++;
+       }
+
+       if (wsi->u.hdr.ah->frags[f].len >= (len - 1))
+               return -1;
+
+       memcpy(dst, &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[f].offset],
+              wsi->u.hdr.ah->frags[f].len);
+       dst[wsi->u.hdr.ah->frags[f].len] = '\0';
+
+       return wsi->u.hdr.ah->frags[f].len;
+}
+
 LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len,
                             enum lws_token_indexes h)
 {