Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void handle_tcp_packet(struct TCB *tcb, const unsigned char *data, unsigned length)
- {
- ...
- for (i=0; i<length; i++)
- tcb->state = http_parse(tcb->state, data[i], &tcb->http);
- ...
- }
- /* States: note that the parser assumes this order of states
- * as it sometimes returns "state + 1". */
- enum {START=0, METHOD, INTER_MU, URI, INTER_UV, VERSION,
- NAME, VALUE, EOL, END_OF_HEADER=-1};
- int http_parse(int state, unsigned char c, struct HttpParser *h)
- {
- static const char WHITESPACE = " \n\r\f\t\v";
- /* ignore nuls and carriage-returns 'cause they complicate
- * stuff */
- if (c == '\0' || c == '\r')
- return state;
- /* Note that the order of states in the switches statement
- * is not the same as the order of states in the enum */
- switch (state) {
- case START:
- if (strchr(WHITESPACE, c)) {
- return state; /* no transition */
- } else {
- search_start(h, METHOD, c);
- return METHOD;
- }
- /* text fields*/
- case METHOD:
- case URL:
- case VERSION:
- case VALUE:
- if (c == '\n') {
- search_end(h);
- return EOL;
- } else if (strchr(WHITESPACE, c)) {
- search_end(h);
- return state+1;
- } else {
- search_continue(h, c);
- return state; /* no transition */
- }
- case NAME:
- if (c == '\n') {
- search_end(h);
- search_start(h, VALUE, '\0');
- search_end(h);
- return EOL;
- } else if (c == ':')) {
- search_end(h);
- search_start(h, VALUE, '\0');
- return VALUE;
- } else {
- search_continue(h, c);
- return state; /* no transition */
- }
- /* space fields */
- case INTER_MV:
- case INTER_UV:
- if (c == '\n') {
- return EOL;
- } else if (strchr(WHITESPACE, c)) {
- return state; /* no transition */
- } else {
- search_start(h, state+1, c);
- return state+1;
- }
- case POST_V:
- if (c == '\n') {
- return EOL;
- } else {
- return state; /* no transition */
- }
- case EOL:
- if (c == '\n') {
- http_request_end(h);
- return END_OF_HEADER;
- } else {
- search_start(h, NAME, c);
- return NAME;
- }
- }
- }
- void search_start(struct HTTP *h, int search_id, int c)
- {
- h->ac_table = aho_corasic_searches[search_id];
- h->ac_state = 0;
- search_continue(h, c);
- }
- void search_continue(struct HTTP *h, unsigned char c)
- {
- int x;
- int row = h->ac_state;
- int column = translate(h, c);
- h->ac_state = h->ac_table[row][column];
- x = ac_match(h->ac_table, h->ac_state);
- if (x)
- report_ids_event(x);
- }
- void search_end(struct HTTP *h)
- {
- search_continue(h, -1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement