Given an HTTP request:
Client: GET /Default.aspx HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave
-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-
xpsdocument, application/xaml+xml, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET
CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Accept-Encoding: gzip, deflate
Host: logan-pc:8080
Connection: Keep-Alive
How can I change the Host
line programmatically? I've tried using regex (using this as a guide) and can't figure out the right pattern.
This is the pattern I'm using now:
".*?Host:.*?:" + <dynamic port number goes here>+ ".*?"
When compiling the pattern I use the Pattern.DOTALL
flag as suggested in the linked question.
UPDATE: I get the request string directly from the browser, this is what I'm using now:
int len = 0;
while((len = ConnHandler.this.clientIn.read(buf)) > 0)
{
System.out.println(".*?Host:.*?:" + ConnHandler.this.serverport+".*?");
String in = new String(buf, 0, len);
in.replaceAll(".*?Host:.*?:" + ConnHandler.this.serverport+".*?", "localhost:" + ConnHandler.this.serverport);
//...
}
Yet it is still not working.