xxxxxxxxxx
enum Type : byte {
None ,
Host ,
ContextLength,
ContentType,
ContentEncoding,
Connection,
CacheControl,
Cookie,
UserAgent,
}
const string[] header_keys = [
"",
"Host",
"Context-Length",
"Context-Type",
"Context-Encoding",
"Connection",
"Cache-Control",
"Cookie",
"User-Agent",
];
struct Header {
string key;
string value;
}
Header[] data = [
{"Host", "examples.com"},
{"Context-Encoding", "gzip"},
{"Content-Type", "text/html; charset=UTF-8"},
{"Cache-Control", "no-cache"},
{"User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"},
];
struct Headers {
Header[] data;
byte[Type.max+1] map = -1 ;
bool parsed = false ;
string get(Type type){
assert( type > 0 && type <= Type.max);
if( !parsed ) {
parsed = true ;
foreach(int i, ref h; data){
foreach(int j, ref k; header_keys) if(j && h.key.length is k.length){
if( strncasecmp(k.ptr, h.key.ptr, k.length) == 0 ) {
map[j] = cast(ubyte) i ;
break;
}
}
}
}
return map[type] >= 0 ? data[ map[type] ].value : null ;
}
}
import core.stdc.stdio;
import core.stdc.string;
extern(C) int strncasecmp(const(char)*, const(char)*, size_t);
extern(C) void main(){
Headers headers ;
headers.data = data ;
auto d = headers.get(Type.Host);
if( d ) {
printf("host=%s\n", d.ptr);
}
}