summaryrefslogtreecommitdiff
path: root/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test.c')
-rw-r--r--test.c87
1 files changed, 82 insertions, 5 deletions
diff --git a/test.c b/test.c
index 3749ce7..792316d 100644
--- a/test.c
+++ b/test.c
@@ -23,15 +23,19 @@ static void die(
exit(EXIT_FAILURE);
}
+/**************/
+/* basic test */
+/**************/
+
static const char *
-str_test_basic =
+basic_str =
"GET / HTTP/1.1\r\n"
"Host: pablotron.org\r\n"
"Connection: close\r\n"
"\r\n";
static bool
-test_basic_cb(
+basic_cb(
fhp_t *fhp,
fhp_token_t token,
uint8_t * const buf,
@@ -59,21 +63,94 @@ test_basic(void) {
fhp_err_t err;
fhp_t fhp;
- if ((err = fhp_init(&fhp, test_basic_cb, NULL)) != FHP_OK) {
+ // init parser
+ if ((err = fhp_init(&fhp, basic_cb, NULL)) != FHP_OK) {
die("test_basic", "fhp_init", err);
}
- size_t len = strlen(str_test_basic);
- if ((err = fhp_push(&fhp, (uint8_t*) str_test_basic, len)) != FHP_OK) {
+ // parse data
+ size_t len = strlen(basic_str);
+ if ((err = fhp_push(&fhp, (uint8_t*) basic_str, len)) != FHP_OK) {
die("test_basic", "fhp_push", err);
}
}
+/****************/
+/* percent test */
+/****************/
+
+typedef struct {
+ uint8_t buf[1024];
+ size_t buf_len;
+} percent_data;
+
+static const char *
+percent_str =
+ "GET /foo%20bar HTTP/1.1\r\n"
+ "Host: pablotron.org\r\n"
+ "Connection: close\r\n"
+ "\r\n";
+
+static bool
+percent_cb(
+ fhp_t *fhp,
+ fhp_token_t token,
+ uint8_t * const buf,
+ size_t len
+) {
+ percent_data *data = fhp_user_data(fhp);
+
+ switch (token) {
+ case FHP_TOKEN_URL_START:
+ // clear buffer
+ data->buf_len = 0;
+
+ break;
+ case FHP_TOKEN_URL_FRAGMENT:
+ // buffer overflow, do not use in real code!!!
+ memcpy(data->buf + data->buf_len, buf, len);
+ data->buf_len += len;
+
+ break;
+ case FHP_TOKEN_URL_END:
+ // terminate and print buffer
+ data->buf[data->buf_len] = '\0';
+ fprintf(stderr, "decoded URL: \"%s\"\n", data->buf);
+
+ break;
+ default:
+ // do nothing
+ NULL;
+ }
+
+ // return success
+ return true;
+}
+
+static void
+test_percent(void) {
+ fhp_err_t err;
+ fhp_t fhp;
+ percent_data data;
+
+ // init parser
+ if ((err = fhp_init(&fhp, percent_cb, &data)) != FHP_OK) {
+ die("test_percent", "fhp_init", err);
+ }
+
+ // parse data
+ size_t len = strlen(percent_str);
+ if ((err = fhp_push(&fhp, (uint8_t*) percent_str, len)) != FHP_OK) {
+ die("test_percent", "fhp_push", err);
+ }
+}
+
int main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
test_basic();
+ test_percent();
return EXIT_SUCCESS;
}