• R/O
  • SSH
  • HTTPS

bchan: 提交


Commit MetaInfo

修訂665 (tree)
時間2015-03-19 00:42:59
作者ornse01

Log Message

implement x-www-form-urlencoded encoder.

Change Summary

差異

--- bchanf/trunk/src/coding/test_htmlform_urlencoder.c (nonexistent)
+++ bchanf/trunk/src/coding/test_htmlform_urlencoder.c (revision 665)
@@ -0,0 +1,158 @@
1+/*
2+ * test_htmlform_urlencoder.c
3+ *
4+ * Copyright (c) 2015 project bchan
5+ *
6+ * This software is provided 'as-is', without any express or implied
7+ * warranty. In no event will the authors be held liable for any damages
8+ * arising from the use of this software.
9+ *
10+ * Permission is granted to anyone to use this software for any purpose,
11+ * including commercial applications, and to alter it and redistribute it
12+ * freely, subject to the following restrictions:
13+ *
14+ * 1. The origin of this software must not be misrepresented; you must not
15+ * claim that you wrote the original software. If you use this software
16+ * in a product, an acknowledgment in the product documentation would be
17+ * appreciated but is not required.
18+ *
19+ * 2. Altered source versions must be plainly marked as such, and must not be
20+ * misrepresented as being the original software.
21+ *
22+ * 3. This notice may not be removed or altered from any source
23+ * distribution.
24+ *
25+ */
26+
27+#include "test_coding.h"
28+
29+#include "htmlform_urlencoder.h"
30+
31+#include <btron/btron.h>
32+#include <bstdio.h>
33+#include <bstring.h>
34+#include <bstdlib.h>
35+
36+#include <unittest_driver.h>
37+
38+typedef struct urlencoder_testdata_t_ {
39+ htmlform_field *src;
40+ W src_len;
41+ UB *expected;
42+ W expected_len;
43+} urlencoder_testdata_t;
44+
45+LOCAL UNITTEST_RESULT test_htmlform_urlencoder_common(urlencoder_testdata_t *testdata)
46+{
47+ htmlform_urlencoder_t encoder;
48+ Bool cont;
49+ UB *str, *result;
50+ W len, result_len;
51+ UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
52+
53+ result = (UB*)malloc(sizeof(UB));
54+ if (result == NULL) {
55+ return UNITTEST_RESULT_FAIL;
56+ }
57+ result[0] = '\0';
58+ result_len = 0;
59+
60+ htmlform_urlencoder_initialize(&encoder, testdata->src, testdata->src_len);
61+
62+ for (;;) {
63+ cont = htmlform_urlencoder_next(&encoder, &str, &len);
64+ if (cont == False) {
65+ break;
66+ }
67+ result = realloc(result, result_len + len + 1);
68+ if (result == NULL) {
69+ ret = UNITTEST_RESULT_FAIL;
70+ break;
71+ }
72+ strncat(result, str, len);
73+ result_len += len;
74+ }
75+
76+ htmlform_urlencoder_finalize(&encoder);
77+
78+ if (result != NULL) {
79+ if (testdata->expected_len != result_len) {
80+ printf("length failure: expected = %d, result = %d\n", testdata->expected_len, result_len);
81+ ret = UNITTEST_RESULT_FAIL;
82+ }
83+ if (strncmp(testdata->expected, result, result_len) != 0) {
84+ printf("encoded string failure: %s\n", result);
85+ ret = UNITTEST_RESULT_FAIL;
86+ }
87+
88+ free(result);
89+ }
90+
91+ return ret;
92+}
93+
94+LOCAL UNITTEST_RESULT test_htmlform_urlencoder_1()
95+{
96+ htmlform_field src[] = {
97+ (htmlform_field){"aaa", 3, "bbb", 3},
98+ (htmlform_field){"cc", 2, "dd", 2},
99+ };
100+ W src_len = 2;
101+ UB expected[] = "aaa=bbb&cc=dd";
102+ W expected_len = strlen(expected);
103+ urlencoder_testdata_t testdata = {
104+ src, src_len, expected, expected_len
105+ };
106+ return test_htmlform_urlencoder_common(&testdata);
107+}
108+
109+LOCAL UNITTEST_RESULT test_htmlform_urlencoder_2()
110+{
111+ htmlform_field src[] = {
112+ (htmlform_field){"aaa", 3, "bb", 2},
113+ (htmlform_field){"cc", 2, "ddd", 3},
114+ };
115+ W src_len = 2;
116+ UB expected[] = "aaa=bb&cc=ddd";
117+ W expected_len = strlen(expected);
118+ urlencoder_testdata_t testdata = {
119+ src, src_len, expected, expected_len
120+ };
121+ return test_htmlform_urlencoder_common(&testdata);
122+}
123+
124+LOCAL UNITTEST_RESULT test_htmlform_urlencoder_3()
125+{
126+ htmlform_field src[] = {
127+ (htmlform_field){"a a", 3, "b b", 3},
128+ };
129+ W src_len = 1;
130+ UB expected[] = "a+a=b+b";
131+ W expected_len = strlen(expected);
132+ urlencoder_testdata_t testdata = {
133+ src, src_len, expected, expected_len
134+ };
135+ return test_htmlform_urlencoder_common(&testdata);
136+}
137+
138+LOCAL UNITTEST_RESULT test_htmlform_urlencoder_4()
139+{
140+ htmlform_field src[] = {
141+ (htmlform_field){(UB[]){0xA0, 0xA1, 0xA2}, 3, (UB[]){0xB3, 0xB4, 0xB5}, 3},
142+ };
143+ W src_len = 1;
144+ UB expected[] = "%A0%A1%A2=%B3%B4%B5";
145+ W expected_len = strlen(expected);
146+ urlencoder_testdata_t testdata = {
147+ src, src_len, expected, expected_len
148+ };
149+ return test_htmlform_urlencoder_common(&testdata);
150+}
151+
152+EXPORT VOID test_htmlform_urlencoder_main(unittest_driver_t *driver)
153+{
154+ UNITTEST_DRIVER_REGIST(driver, test_htmlform_urlencoder_1);
155+ UNITTEST_DRIVER_REGIST(driver, test_htmlform_urlencoder_2);
156+ UNITTEST_DRIVER_REGIST(driver, test_htmlform_urlencoder_3);
157+ UNITTEST_DRIVER_REGIST(driver, test_htmlform_urlencoder_4);
158+}
--- bchanf/trunk/src/coding/htmlform_urlencoder.c (nonexistent)
+++ bchanf/trunk/src/coding/htmlform_urlencoder.c (revision 665)
@@ -0,0 +1,137 @@
1+/*
2+ * htmlform_urlencoder.c
3+ *
4+ * Copyright (c) 2015 project bchan
5+ *
6+ * This software is provided 'as-is', without any express or implied
7+ * warranty. In no event will the authors be held liable for any damages
8+ * arising from the use of this software.
9+ *
10+ * Permission is granted to anyone to use this software for any purpose,
11+ * including commercial applications, and to alter it and redistribute it
12+ * freely, subject to the following restrictions:
13+ *
14+ * 1. The origin of this software must not be misrepresented; you must not
15+ * claim that you wrote the original software. If you use this software
16+ * in a product, an acknowledgment in the product documentation would be
17+ * appreciated but is not required.
18+ *
19+ * 2. Altered source versions must be plainly marked as such, and must not be
20+ * misrepresented as being the original software.
21+ *
22+ * 3. This notice may not be removed or altered from any source
23+ * distribution.
24+ *
25+ */
26+
27+#include "htmlform_urlencoder.h"
28+
29+#include <basic.h>
30+#include <bctype.h>
31+
32+#ifdef BCHAN_CONFIG_DEBUG
33+# define DP(arg) printf arg
34+# define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
35+#else
36+# define DP(arg) /**/
37+# define DP_ER(msg, err) /**/
38+#endif
39+
40+#if 0
41+# define DP_STATE(state) printf("%s\n", state)
42+#else
43+# define DP_STATE(state) /**/
44+#endif
45+
46+LOCAL VOID htmlform_urlencoder_convert(htmlform_urlencoder_t *encoder, UB ch, UB **str, W *len)
47+{
48+ static UB num[] = "0123456789ABCDEF";
49+ UB *buffer = encoder->buf;
50+
51+ if (ch == 0x2E) { /* '.' */
52+ buffer[0] = ch;
53+ *len = 1;
54+ } else if (ch == 0x2D) { /* '-' */
55+ buffer[0] = ch;
56+ *len = 1;
57+ } else if (ch == 0x5F) { /* '_' */
58+ buffer[0] = ch;
59+ *len = 1;
60+ } else if (ch == 0x20) { /* ' ' */
61+ buffer[0] = 0x2B; /* '+' */
62+ *len = 1;
63+ } else if (isalnum(ch)) {
64+ buffer[0] = ch;
65+ *len = 1;
66+ } else {
67+ buffer[0] = 0x25; /* '%' */
68+ buffer[1] = num[ch >> 4];
69+ buffer[2] = num[ch & 0xF];
70+ *len = 3;
71+ }
72+
73+ *str = buffer;
74+}
75+
76+LOCAL UB equal[] = "=";
77+LOCAL UB amp[] = "&";
78+
79+EXPORT Bool htmlform_urlencoder_next(htmlform_urlencoder_t *encoder, UB **str, W *len)
80+{
81+ htmlform_field *field;
82+
83+ if (encoder->field_index == encoder->fields_len) {
84+ return False;
85+ }
86+
87+ field = encoder->fields + encoder->field_index;
88+
89+ switch (encoder->state) {
90+ case HTMLFORM_URLENCODER_STATE_NAME:
91+ DP_STATE("NAME");
92+ htmlform_urlencoder_convert(encoder, field->name[encoder->src_pos], str, len);
93+ encoder->src_pos++;
94+ if (encoder->src_pos == field->name_len) {
95+ encoder->state = HTMLFORM_URLENCODER_STATE_EQUAL;
96+ }
97+ return True;
98+ case HTMLFORM_URLENCODER_STATE_EQUAL:
99+ DP_STATE("EQUAL");
100+ *str = equal;
101+ *len = 1;
102+ encoder->state = HTMLFORM_URLENCODER_STATE_VALUE;
103+ encoder->src_pos = 0;
104+ return True;
105+ case HTMLFORM_URLENCODER_STATE_VALUE:
106+ DP_STATE("VALUE");
107+ htmlform_urlencoder_convert(encoder, field->value[encoder->src_pos], str, len);
108+ encoder->src_pos++;
109+ if (encoder->src_pos == field->value_len) {
110+ encoder->state = HTMLFORM_URLENCODER_STATE_AMP;
111+ encoder->field_index++;
112+ }
113+ return True;
114+ case HTMLFORM_URLENCODER_STATE_AMP:
115+ DP_STATE("AMP");
116+ *str = amp;
117+ *len = 1;
118+ encoder->state = HTMLFORM_URLENCODER_STATE_NAME;
119+ encoder->src_pos = 0;
120+ return True;
121+ }
122+
123+ return False;
124+}
125+
126+EXPORT VOID htmlform_urlencoder_initialize(htmlform_urlencoder_t *encoder, htmlform_field *fields, W fields_len)
127+{
128+ encoder->fields = fields;
129+ encoder->fields_len = fields_len;
130+ encoder->state = HTMLFORM_URLENCODER_STATE_NAME;
131+ encoder->field_index = 0;
132+ encoder->src_pos = 0;
133+}
134+
135+EXPORT VOID htmlform_urlencoder_finalize(htmlform_urlencoder_t *encoder)
136+{
137+}
--- bchanf/trunk/src/coding/htmlform_urlencoder.h (nonexistent)
+++ bchanf/trunk/src/coding/htmlform_urlencoder.h (revision 665)
@@ -0,0 +1,70 @@
1+/*
2+ * htmlform_urlencoder.h
3+ *
4+ * Copyright (c) 2015 project bchan
5+ *
6+ * This software is provided 'as-is', without any express or implied
7+ * warranty. In no event will the authors be held liable for any damages
8+ * arising from the use of this software.
9+ *
10+ * Permission is granted to anyone to use this software for any purpose,
11+ * including commercial applications, and to alter it and redistribute it
12+ * freely, subject to the following restrictions:
13+ *
14+ * 1. The origin of this software must not be misrepresented; you must not
15+ * claim that you wrote the original software. If you use this software
16+ * in a product, an acknowledgment in the product documentation would be
17+ * appreciated but is not required.
18+ *
19+ * 2. Altered source versions must be plainly marked as such, and must not be
20+ * misrepresented as being the original software.
21+ *
22+ * 3. This notice may not be removed or altered from any source
23+ * distribution.
24+ *
25+ */
26+
27+/* http://tools.ietf.org/html/rfc1866#section-8.2.1 */
28+
29+/* Vendor name: */
30+/* Functionality name: htmlform */
31+/* Detail name: urnencoder */
32+
33+#include <basic.h>
34+
35+#ifndef __HTMLFORM_URLENCODER_H__
36+#define __HTMLFORM_URLENCODER_H__
37+
38+/* Functionality name: htmlform */
39+/* Detail name: */
40+/* Data structure identifier: field */
41+struct htmlform_field_ {
42+ UB *name;
43+ W name_len;
44+ UB *value;
45+ W value_len;
46+};
47+typedef struct htmlform_field_ htmlform_field;
48+
49+/* Functionality name: htmlform */
50+/* Detail name: urlencoder */
51+struct htmlform_urlencoder_t_ {
52+ htmlform_field *fields;
53+ W fields_len;
54+ enum {
55+ HTMLFORM_URLENCODER_STATE_NAME,
56+ HTMLFORM_URLENCODER_STATE_EQUAL,
57+ HTMLFORM_URLENCODER_STATE_VALUE,
58+ HTMLFORM_URLENCODER_STATE_AMP
59+ } state;
60+ W field_index;
61+ W src_pos;
62+ UB buf[3];
63+};
64+typedef struct htmlform_urlencoder_t_ htmlform_urlencoder_t;
65+
66+IMPORT VOID htmlform_urlencoder_initialize(htmlform_urlencoder_t *encoder, htmlform_field *fields, W fields_len);
67+IMPORT VOID htmlform_urlencoder_finalize(htmlform_urlencoder_t *encoder);
68+IMPORT Bool htmlform_urlencoder_next(htmlform_urlencoder_t *encoder, UB **str, W *len);
69+
70+#endif
Show on old repository browser