私はhttpパーサーを書いていて、これらの機能を持っています
int parse_useragent(char* buf, int length){
buf[length] = '\0';
if(strstr(buf, "MSIE") != NULL){
return 1;
}else if(strstr(buf, "Firefox") != NULL){
return 2;
}
return DEFAULT_USERAGENT;
}
void parse_headers(unsigned char* buf, http_record_t * http){
char * position = (char*)buf;
char referer[] = "Referer";
char useragent[] = "User-Agent";
...
int length = getlinelength(position); // returns length of line
while(length != 1){ // position points to start of line every iteration of cycle
if(strncmp(position, useragent, sizeof(useragent)-1) == 0){
http->useragent = parse_useragent(position, length);
fprintf(stderr,"parsing useragent \n");
}else if(strncmp(position, referer, sizeof(referer)-1) == 0){
fprintf(stderr,"parsing referer \n");
char * tmp = malloc(REFERER_LENGHT * sizeof(char));
parse_referer(tmp,position, length);
strncpy(http->referer,tmp, REFERER_LENGHT * sizeof(char) - 1);
}else if(...
position += length + 1;
length = getlinelength(position);
}
return;
}
buf
httpヘッダーの先頭を指します。
私は各ヘッダーのような機能を持っておりparse_useragent
、本当にそれらを最適化する必要があります。パケットの長さは通常<1000であり、行の長さが100の値を超えることはめったにありません。このような短い文字列の最適化は、顕著な効果をもたらしますか?
これらのアルゴリズムの中には、行ごとに解析するという異なるアプローチが必要なものがあることを私は知っています。これらの特定の条件下でどちらを選択しますか?
- http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
- http://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_string_search_algorithm
- http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
- http://en.wikipedia.org/wiki/Suffix_tree
- http://en.wikipedia.org/wiki/Suffix_array
- http://www.codeproject.com/Articles/250566/Fastest-strstr-like-function-in-C
- http://www.sanmayce.com/Railgun/index.html
手伝ってくれてありがとう!