Commit 81d98966a4301ea284acef97297613cd6bbae8bc

Authored by Georg Hopp
1 parent f1bf7c49

porformance improvement in parsing process (no longer do alloc and free on each line)

Showing 1 changed file with 22 additions and 38 deletions
... ... @@ -101,49 +101,28 @@ CREATE_CLASS(HttpRequestParser, NULL, IFACE(Class), IFACE(StreamReader));
101 101
102 102 static
103 103 inline
104   -int
105   -httpRequestLineGet(HttpRequestParser this, char ** line)
  104 +char *
  105 +httpRequestLineGet(char ** data)
106 106 {
107   - char * line_end = strstr(this->buffer, "\r\n");
  107 + char * line_end = strstr(*data, "\r\n");
  108 + char * ret = *data;
108 109
109 110 if (NULL == line_end) {
110   - *line = NULL;
111   - return -1;
  111 + return NULL;
112 112 }
113 113
114 114 *line_end = 0;
115   - *line = malloc(strlen(this->buffer) + 1);
116   - strcpy(*line, this->buffer);
  115 + *data = line_end + 2;
117 116
118   - line_end+=2;
119   -
120   - memmove(this->buffer,
121   - line_end,
122   - this->buffer_used - (line_end-this->buffer) + 1);
123   -
124   - this->buffer_used -= line_end-this->buffer;
125   -
126   - return line_end-this->buffer-2;
  117 + return ret;
127 118 }
128 119
129 120 static
130 121 inline
131 122 void
132   -httpRequestSkip(HttpRequestParser this)
  123 +httpRequestSkip(char ** data)
133 124 {
134   - size_t skip;
135   -
136   - for (skip = 0;
137   - 0 != this->buffer[skip]
138   - && ! isalpha(this->buffer[skip])
139   - && this->buffer_used > skip;
140   - skip++);
141   -
142   - memmove(this->buffer,
143   - &this->buffer[skip],
144   - this->buffer_used - skip + 1);
145   -
146   - this->buffer_used -= skip;
  125 + for (; 0 != **data && ! isalpha(**data); *data++);
147 126 }
148 127
149 128 static
... ... @@ -151,9 +130,9 @@ void
151 130 httpRequestParserParse(HttpRequestParser this)
152 131 {
153 132 //static HttpRequest request = NULL;
  133 + char * data = this->buffer;
154 134 char * line;
155 135 int cont = 1;
156   - int length;
157 136
158 137 //if(NULL == HttpRequest) {
159 138 // request = new(HttpRequest);
... ... @@ -163,38 +142,35 @@ httpRequestParserParse(HttpRequestParser this)
163 142 switch(this->state) {
164 143 case HTTP_REQUEST_GARBAGE:
165 144 puts("==skip garbage==");
166   - httpRequestSkip(this);
  145 + httpRequestSkip(&data);
167 146
168 147 this->state = HTTP_REQUEST_START;
169 148 break;
170 149
171 150 case HTTP_REQUEST_START:
172 151 puts("==request line==");
173   - if (-1 == httpRequestLineGet(this, &line)) {
  152 + if (NULL == (line = httpRequestLineGet(&data))) {
174 153 cont = 0;
175 154 break;
176 155 }
177 156
178 157 printf("%s\n", line);
179   - free(line);
180 158 this->state = HTTP_REQUEST_REQEUST_LINE_DONE;
181 159 break;
182 160
183 161 case HTTP_REQUEST_REQEUST_LINE_DONE:
184 162 puts("==read header==");
185   - if (-1 == (length = httpRequestLineGet(this, &line))) {
  163 + if (NULL == (line = httpRequestLineGet(&data))) {
186 164 cont = 0;
187 165 break;
188 166 }
189 167
190   - if (0 == length) {
191   - free(line);
  168 + if (0 == strlen(line)) {
192 169 this->state = HTTP_REQUEST_HEADERS_DONE;
193 170 break;
194 171 }
195 172
196 173 printf("%s\n", line);
197   - free(line);
198 174 break;
199 175
200 176 case HTTP_REQUEST_HEADERS_DONE:
... ... @@ -205,10 +181,18 @@ httpRequestParserParse(HttpRequestParser this)
205 181 case HTTP_REQUEST_DONE:
206 182 puts("==request done==");
207 183
  184 + memmove(this->buffer,
  185 + data,
  186 + this->buffer_used - (data - this->buffer) + 1);
  187 +
  188 + this->buffer_used -= data - this->buffer;
  189 +
208 190 if (0 == this->buffer_used) {
209 191 cont = 0;
210 192 }
211 193
  194 + this->state = HTTP_REQUEST_GARBAGE;
  195 +
212 196 break;
213 197
214 198 default:
... ...
Please register or login to post a comment