1

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.

Community
  • 1
  • 1
ldam
  • 4,412
  • 6
  • 45
  • 76

2 Answers2

0

Your pattern is correct. This code works for me:

String string = "Accept-Encoding: gzip, deflate\nHost: logan-pc:8080\nConnection: Keep-Alive";
System.out.println(string.replaceAll(".*?Host:.*?:8080.*?", "Host: NEW_HOST:8080"));

Output:

Accept-Encoding: gzip, deflate

Host: NEW_HOST:8080

Connection: Keep-Alive

Community
  • 1
  • 1
Taky
  • 5,284
  • 1
  • 20
  • 29
  • Still not working for me. I'm getting the request string directly from the browser via an input stream and redirecting it to another, perhaps there's something else going on in there. I'll update my question. – ldam Jan 18 '13 at 13:29
  • Solved it. I just needed to `in = in.replaceAll(...)` – ldam Jan 18 '13 at 14:13
0

Replace: /static/cache/gettheme_whiteboard_ with: /static/cache/gettheme_cust

Fabiano
  • 5,124
  • 6
  • 42
  • 69