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,49 +101,28 @@ CREATE_CLASS(HttpRequestParser, NULL, IFACE(Class), IFACE(StreamReader));
101 101
102 static 102 static
103 inline 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 if (NULL == line_end) { 110 if (NULL == line_end) {
110 - *line = NULL;  
111 - return -1; 111 + return NULL;
112 } 112 }
113 113
114 *line_end = 0; 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 static 120 static
130 inline 121 inline
131 void 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 static 128 static
@@ -151,9 +130,9 @@ void @@ -151,9 +130,9 @@ void
151 httpRequestParserParse(HttpRequestParser this) 130 httpRequestParserParse(HttpRequestParser this)
152 { 131 {
153 //static HttpRequest request = NULL; 132 //static HttpRequest request = NULL;
  133 + char * data = this->buffer;
154 char * line; 134 char * line;
155 int cont = 1; 135 int cont = 1;
156 - int length;  
157 136
158 //if(NULL == HttpRequest) { 137 //if(NULL == HttpRequest) {
159 // request = new(HttpRequest); 138 // request = new(HttpRequest);
@@ -163,38 +142,35 @@ httpRequestParserParse(HttpRequestParser this) @@ -163,38 +142,35 @@ httpRequestParserParse(HttpRequestParser this)
163 switch(this->state) { 142 switch(this->state) {
164 case HTTP_REQUEST_GARBAGE: 143 case HTTP_REQUEST_GARBAGE:
165 puts("==skip garbage=="); 144 puts("==skip garbage==");
166 - httpRequestSkip(this); 145 + httpRequestSkip(&data);
167 146
168 this->state = HTTP_REQUEST_START; 147 this->state = HTTP_REQUEST_START;
169 break; 148 break;
170 149
171 case HTTP_REQUEST_START: 150 case HTTP_REQUEST_START:
172 puts("==request line=="); 151 puts("==request line==");
173 - if (-1 == httpRequestLineGet(this, &line)) { 152 + if (NULL == (line = httpRequestLineGet(&data))) {
174 cont = 0; 153 cont = 0;
175 break; 154 break;
176 } 155 }
177 156
178 printf("%s\n", line); 157 printf("%s\n", line);
179 - free(line);  
180 this->state = HTTP_REQUEST_REQEUST_LINE_DONE; 158 this->state = HTTP_REQUEST_REQEUST_LINE_DONE;
181 break; 159 break;
182 160
183 case HTTP_REQUEST_REQEUST_LINE_DONE: 161 case HTTP_REQUEST_REQEUST_LINE_DONE:
184 puts("==read header=="); 162 puts("==read header==");
185 - if (-1 == (length = httpRequestLineGet(this, &line))) { 163 + if (NULL == (line = httpRequestLineGet(&data))) {
186 cont = 0; 164 cont = 0;
187 break; 165 break;
188 } 166 }
189 167
190 - if (0 == length) {  
191 - free(line); 168 + if (0 == strlen(line)) {
192 this->state = HTTP_REQUEST_HEADERS_DONE; 169 this->state = HTTP_REQUEST_HEADERS_DONE;
193 break; 170 break;
194 } 171 }
195 172
196 printf("%s\n", line); 173 printf("%s\n", line);
197 - free(line);  
198 break; 174 break;
199 175
200 case HTTP_REQUEST_HEADERS_DONE: 176 case HTTP_REQUEST_HEADERS_DONE:
@@ -205,10 +181,18 @@ httpRequestParserParse(HttpRequestParser this) @@ -205,10 +181,18 @@ httpRequestParserParse(HttpRequestParser this)
205 case HTTP_REQUEST_DONE: 181 case HTTP_REQUEST_DONE:
206 puts("==request done=="); 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 if (0 == this->buffer_used) { 190 if (0 == this->buffer_used) {
209 cont = 0; 191 cont = 0;
210 } 192 }
211 193
  194 + this->state = HTTP_REQUEST_GARBAGE;
  195 +
212 break; 196 break;
213 197
214 default: 198 default:
Please register or login to post a comment