Przeglądaj źródła

Added mg_get_var2() to parse repeated query strings

Thomas Davis 12 lat temu
rodzic
commit
ff6662c406
3 zmienionych plików z 32 dodań i 1 usunięć
  1. 1 0
      RELEASE_NOTES.md
  2. 25 0
      include/civetweb.h
  3. 6 1
      src/civetweb.c

+ 1 - 0
RELEASE_NOTES.md

@@ -2,6 +2,7 @@ Release Notes v1.4 (UNDER DEVELOPMENT)
 ===
 ===
 ### Objectives: *???*
 ### Objectives: *???*
 
 
+- Added mg_get_var2() to parse repeated query variables
 - Externalized logging function cry() as mg_cry()
 - Externalized logging function cry() as mg_cry()
 - Added CivetServer::getCookie method (Hariprasad Kamath)
 - Added CivetServer::getCookie method (Hariprasad Kamath)
 - Added CivetServer::getHeader method (Hariprasad Kamath)
 - Added CivetServer::getHeader method (Hariprasad Kamath)

+ 25 - 0
include/civetweb.h

@@ -314,6 +314,31 @@ const char *mg_get_header(const struct mg_connection *, const char *name);
 int mg_get_var(const char *data, size_t data_len,
 int mg_get_var(const char *data, size_t data_len,
                const char *var_name, char *dst, size_t dst_len);
                const char *var_name, char *dst, size_t dst_len);
 
 
+// Get a value of particular form variable.
+//
+// Parameters:
+//   data: pointer to form-uri-encoded buffer. This could be either POST data,
+//         or request_info.query_string.
+//   data_len: length of the encoded data.
+//   var_name: variable name to decode from the buffer
+//   dst: destination buffer for the decoded variable
+//   dst_len: length of the destination buffer
+//   occurrence: which occurrence of the variable, 0 is the first, 1 the second...
+//              this makes it possible to parse a query like
+//              b=x&a=y&a=z which will have occurrence values b:0, a:0 and a:1
+//
+// Return:
+//   On success, length of the decoded variable.
+//   On error:
+//      -1 (variable not found).
+//      -2 (destination buffer is NULL, zero length or too small to hold the
+//          decoded variable).
+//
+// Destination buffer is guaranteed to be '\0' - terminated if it is not
+// NULL or zero length.
+int mg_get_var2(const char *data, size_t data_len,
+               const char *var_name, char *dst, size_t dst_len, size_t occurrence);
+
 // Fetch value of certain cookie variable into the destination buffer.
 // Fetch value of certain cookie variable into the destination buffer.
 //
 //
 // Destination buffer is guaranteed to be '\0' - terminated. In case of
 // Destination buffer is guaranteed to be '\0' - terminated. In case of

+ 6 - 1
src/civetweb.c

@@ -1724,6 +1724,11 @@ int mg_url_decode(const char *src, int src_len, char *dst,
 
 
 int mg_get_var(const char *data, size_t data_len, const char *name,
 int mg_get_var(const char *data, size_t data_len, const char *name,
                char *dst, size_t dst_len) {
                char *dst, size_t dst_len) {
+   return mg_get_var2(data,data_len,name,dst,dst_len,0);
+}
+
+int mg_get_var2(const char *data, size_t data_len, const char *name,
+               char *dst, size_t dst_len, size_t occurrence) {
   const char *p, *e, *s;
   const char *p, *e, *s;
   size_t name_len;
   size_t name_len;
   int len;
   int len;
@@ -1742,7 +1747,7 @@ int mg_get_var(const char *data, size_t data_len, const char *name,
     // data is "var1=val1&var2=val2...". Find variable first
     // data is "var1=val1&var2=val2...". Find variable first
     for (p = data; p + name_len < e; p++) {
     for (p = data; p + name_len < e; p++) {
       if ((p == data || p[-1] == '&') && p[name_len] == '=' &&
       if ((p == data || p[-1] == '&') && p[name_len] == '=' &&
-          !mg_strncasecmp(name, p, name_len)) {
+          !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) {
 
 
         // Point p to variable value
         // Point p to variable value
         p += name_len + 1;
         p += name_len + 1;