OSDN Git Service

fix for bchanf convention.
[bbk/bchanf.git] / src / http / http_headerlexer.c
1 /*
2  * http_headerlexer.c
3  *
4  * Copyright (c) 2011-2012 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    "http_headerlexer.h"
28
29 #include        <basic.h>
30 #include        <bstdlib.h>
31 #include        <bstdio.h>
32
33 #ifdef BCHAN_CONFIG_DEBUG
34 # define DP(arg) printf arg
35 # define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
36 #else
37 # define DP(arg) /**/
38 # define DP_ER(msg, err) /**/
39 #endif
40
41 #if 0
42 #define DP_STATE(arg) printf arg
43 #else
44 #define DP_STATE(arg) /**/
45 #endif
46
47 EXPORT VOID http_headerlexer_inputchar(http_headerlexer_t *lexer, UB ch, HTTP_HEADERLEXER_RESULT *result)
48 {
49         *result = HTTP_HEADERLEXER_RESULT_NONE;
50
51         switch (lexer->state) {
52         case HTTP_HEADERLEXER_STATE_SEARCH_HEADER:
53                 DP_STATE(("state = SEARCH_HEADER: %c[%02x]\n", ch, ch));
54                 if (ch == '\r') {
55                         lexer->state = HTTP_HEADERLEXER_STATE_SEARCH_HEADER_CR;
56                         break;
57                 }
58                 if ((ch == ' ')&&(ch = '\t')) {
59                         lexer->state = HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDVALUE_LWS;
60                         *result = HTTP_HEADERLEXER_RESULT_LWS;
61                         /* TODO: return value in "LWS CRLF LWS". */
62                         break;
63                 }
64                 /* TODO: handling error charactors for "token" */
65                 lexer->state = HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDNAME;
66                 *result = HTTP_HEADERLEXER_RESULT_FIELDNAME;
67                 break;
68         case HTTP_HEADERLEXER_STATE_SEARCH_HEADER_CR:
69                 DP_STATE(("state = SEARCH_HEADER_CR: %c[%02x]\n", ch, ch));
70                 if (ch == '\n') {
71                         lexer->state = HTTP_HEADERLEXER_STATE_READ_VALUE_MESSAGE;
72                         *result = HTTP_HEADERLEXER_RESULT_MESSAGEHEADER_END;
73                         break;
74                 }
75                 /* TODO: below error case. */
76                 if ((ch == ' ')&&(ch = '\t')) {
77                         lexer->state = HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDVALUE_LWS;
78                         *result = HTTP_HEADERLEXER_RESULT_LWS;
79                         break;
80                 }
81                 lexer->state = HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDNAME;
82                 *result = HTTP_HEADERLEXER_RESULT_FIELDNAME;
83                 break;
84         case HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDNAME:
85                 DP_STATE(("state = READ_HEADER_FIELDNAME: %c[%02x]\n", ch, ch));
86                 if (ch == '\r') {
87                         lexer->state = HTTP_HEADERLEXER_STATE_HERDER_CR;
88                         break;
89                 }
90                 if (ch == ':') {
91                         lexer->state = HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDVALUE;
92                         *result = HTTP_HEADERLEXER_RESULT_FIELDNAME_END;
93                         break;
94                 }
95                 /* TODO: handling error charactors for "token" */
96                 *result = HTTP_HEADERLEXER_RESULT_FIELDNAME;
97                 break;
98         case HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDVALUE:
99                 DP_STATE(("state = READ_HEADER_FIELDVALUE: %c[%02x]\n", ch, ch));
100                 if (ch == '\r') {
101                         lexer->state = HTTP_HEADERLEXER_STATE_HERDER_CR;
102                         break;
103                 }
104                 if ((ch == ' ')&&(ch = '\t')) {
105                         lexer->state = HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDVALUE_LWS;
106                         *result = HTTP_HEADERLEXER_RESULT_LWS;
107                         break;
108                 }
109                 *result = HTTP_HEADERLEXER_RESULT_FIELDCONTENT;
110                 break;
111         case HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDVALUE_LWS:
112                 DP_STATE(("state = READ_HEADER_FIELDVALUE_LWS: %c[%02x]\n", ch, ch));
113                 if (ch == '\r') {
114                         lexer->state = HTTP_HEADERLEXER_STATE_HERDER_CR;
115                         break;
116                 }
117                 if ((ch != ' ')&&(ch != '\t')) {
118                         lexer->state = HTTP_HEADERLEXER_STATE_READ_HEADER_FIELDVALUE;
119                         *result = HTTP_HEADERLEXER_RESULT_FIELDCONTENT;
120                         break;
121                 }
122                 break;
123         case HTTP_HEADERLEXER_STATE_HERDER_CR:
124                 DP_STATE(("state = HERDER_CR: %c[%02x]\n", ch, ch));
125                 if (ch == '\n') {
126                         lexer->state = HTTP_HEADERLEXER_STATE_SEARCH_HEADER;
127                 }
128                 break;
129         case HTTP_HEADERLEXER_STATE_READ_VALUE_MESSAGE:
130                 DP_STATE(("state = READ_VALUE_MESSAGE: %c[%02x]\n", ch, ch));
131                 break;
132         }
133         return;
134 }
135
136 EXPORT W http_headerlexer_initialize(http_headerlexer_t *lexer)
137 {
138         lexer->state = HTTP_HEADERLEXER_STATE_SEARCH_HEADER;
139         return 0;
140 }
141
142 EXPORT VOID http_headerlexer_finalize(http_headerlexer_t *lexer)
143 {
144 }