aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-05-08 17:24:19 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-05-09 22:20:42 +0300
commite93439da71e027cafe5b2788a997cbfc85d193c8 (patch)
treea4004d13b98d12b1529eec696c9ef1d2401331c5 /src
parentb03ee4c52a6deebdcad3f12a18c4bd4f947f167b (diff)
downloadlibmicrohttpd-e93439da71e027cafe5b2788a997cbfc85d193c8.tar.gz
libmicrohttpd-e93439da71e027cafe5b2788a997cbfc85d193c8.zip
Added function for detection of token inside comma-separated string, added tests
Diffstat (limited to 'src')
-rw-r--r--src/microhttpd/Makefile.am4
-rw-r--r--src/microhttpd/internal.h3
-rw-r--r--src/microhttpd/mhd_str.c58
-rw-r--r--src/microhttpd/mhd_str.h42
4 files changed, 107 insertions, 0 deletions
diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am
index 3f1b828f..36121539 100644
--- a/src/microhttpd/Makefile.am
+++ b/src/microhttpd/Makefile.am
@@ -142,6 +142,7 @@ endif
142check_PROGRAMS = \ 142check_PROGRAMS = \
143 test_str_compare \ 143 test_str_compare \
144 test_str_to_value \ 144 test_str_to_value \
145 test_str_token \
145 test_http_reasons \ 146 test_http_reasons \
146 test_shutdown_select \ 147 test_shutdown_select \
147 test_shutdown_poll \ 148 test_shutdown_poll \
@@ -251,6 +252,9 @@ test_str_compare_SOURCES = \
251test_str_to_value_SOURCES = \ 252test_str_to_value_SOURCES = \
252 test_str.c test_helpers.h mhd_str.c 253 test_str.c test_helpers.h mhd_str.c
253 254
255test_str_token_SOURCES = \
256 test_str_token.c mhd_str.c
257
254test_http_reasons_SOURCES = \ 258test_http_reasons_SOURCES = \
255 test_http_reasons.c \ 259 test_http_reasons.c \
256 reason_phrase.c mhd_str.c mhd_str.h 260 reason_phrase.c mhd_str.c mhd_str.h
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
index cb6753c9..a9c46c6d 100644
--- a/src/microhttpd/internal.h
+++ b/src/microhttpd/internal.h
@@ -117,10 +117,13 @@ extern void *mhd_panic_cls;
117#define BUILTIN_NOT_REACHED 117#define BUILTIN_NOT_REACHED
118#endif 118#endif
119 119
120#ifndef MHD_STATICSTR_LEN_
120/** 121/**
121 * Determine length of static string / macro strings at compile time. 122 * Determine length of static string / macro strings at compile time.
122 */ 123 */
123#define MHD_STATICSTR_LEN_(macro) (sizeof(macro)/sizeof(char) - 1) 124#define MHD_STATICSTR_LEN_(macro) (sizeof(macro)/sizeof(char) - 1)
125#endif /* ! MHD_STATICSTR_LEN_ */
126
124 127
125/** 128/**
126 * State of the socket with respect to epoll (bitmask). 129 * State of the socket with respect to epoll (bitmask).
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index c5e9813c..bb10d26f 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -365,6 +365,64 @@ MHD_str_equal_caseless_n_ (const char * const str1,
365 return !0; 365 return !0;
366} 366}
367 367
368
369/**
370 * Check whether @a str has case-insensitive @a token.
371 * Token could be surrounded by spaces and tabs and delimited by comma.
372 * Match succeed if substring between start, end (of string) or comma
373 * contains only case-insensitive token and optional spaces and tabs.
374 * @warning token must not contain null-charters except optional
375 * terminating null-character.
376 * @param str the string to check
377 * @param token the token to find
378 * @param token_len length of token, not including optional terminating
379 * null-character.
380 * @return non-zero if two strings are equal, zero otherwise.
381 */
382bool
383MHD_str_has_token_caseless_ (const char * str,
384 const char * const token,
385 size_t token_len)
386{
387 if (0 == token_len)
388 return false;
389
390 while (0 != *str)
391 {
392 size_t i;
393 /* Skip all whitespaces and empty tokens. */
394 while (' ' == *str || '\t' == *str || ',' == *str) str++;
395
396 /* Check for token match. */
397 i = 0;
398 while (1)
399 {
400 const char sc = *(str++);
401 const char tc = token[i++];
402
403 if (0 == sc)
404 return false;
405 if ( (sc != tc) &&
406 (toasciilower (sc) != toasciilower (tc)) )
407 break;
408 if (i >= token_len)
409 {
410 /* Check whether substring match token fully or
411 * has additional unmatched chars at tail. */
412 while (' ' == *str || '\t' == *str) str++;
413 /* End of (sub)string? */
414 if (0 == *str || ',' == *str)
415 return true;
416 /* Unmatched chars at end of substring. */
417 break;
418 }
419 }
420 /* Find next substring. */
421 while (0 != *str && ',' != *str) str++;
422 }
423 return false;
424}
425
368#ifndef MHD_FAVOR_SMALL_CODE 426#ifndef MHD_FAVOR_SMALL_CODE
369/* Use individual function for each case */ 427/* Use individual function for each case */
370 428
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h
index 0ee41717..410cc36e 100644
--- a/src/microhttpd/mhd_str.h
+++ b/src/microhttpd/mhd_str.h
@@ -30,11 +30,21 @@
30 30
31#include <stdint.h> 31#include <stdint.h>
32#include <stdlib.h> 32#include <stdlib.h>
33#ifdef HAVE_STDBOOL_H
34#include <stdbool.h>
35#endif /* HAVE_STDBOOL_H */
33 36
34#ifdef MHD_FAVOR_SMALL_CODE 37#ifdef MHD_FAVOR_SMALL_CODE
35#include "mhd_limits.h" 38#include "mhd_limits.h"
36#endif /* MHD_FAVOR_SMALL_CODE */ 39#endif /* MHD_FAVOR_SMALL_CODE */
37 40
41#ifndef MHD_STATICSTR_LEN_
42/**
43 * Determine length of static string / macro strings at compile time.
44 */
45#define MHD_STATICSTR_LEN_(macro) (sizeof(macro)/sizeof(char) - 1)
46#endif /* ! MHD_STATICSTR_LEN_ */
47
38/* 48/*
39 * Block of functions/macros that use US-ASCII charset as required by HTTP 49 * Block of functions/macros that use US-ASCII charset as required by HTTP
40 * standards. Not affected by current locale settings. 50 * standards. Not affected by current locale settings.
@@ -71,6 +81,38 @@ MHD_str_equal_caseless_n_ (const char * const str1,
71 const char * const str2, 81 const char * const str2,
72 size_t maxlen); 82 size_t maxlen);
73 83
84
85/**
86 * Check whether @a str has case-insensitive @a token.
87 * Token could be surrounded by spaces and tabs and delimited by comma.
88 * Match succeed if substring between start, end of string or comma
89 * contains only case-insensitive token and optional spaces and tabs.
90 * @warning token must not contain null-charters except optional
91 * terminating null-character.
92 * @param str the string to check
93 * @param token the token to find
94 * @param token_len length of token, not including optional terminating
95 * null-character.
96 * @return non-zero if two strings are equal, zero otherwise.
97 */
98bool
99MHD_str_has_token_caseless_ (const char * str,
100 const char * const token,
101 size_t token_len);
102
103/**
104 * Check whether @a str has case-insensitive static @a tkn.
105 * Token could be surrounded by spaces and tabs and delimited by comma.
106 * Match succeed if substring between start, end of string or comma
107 * contains only case-insensitive token and optional spaces and tabs.
108 * @warning tkn must be static string
109 * @param str the string to check
110 * @param tkn the static string of token to find
111 * @return non-zero if two strings are equal, zero otherwise.
112 */
113#define MHD_str_has_s_token_caseless_(str,tkn) \
114 MHD_str_has_token_caseless_((str),(tkn),MHD_STATICSTR_LEN_(tkn))
115
74#ifndef MHD_FAVOR_SMALL_CODE 116#ifndef MHD_FAVOR_SMALL_CODE
75/* Use individual function for each case to improve speed */ 117/* Use individual function for each case to improve speed */
76 118