Philip Langford
Philip Langford

Reputation: 106

What is a good method to inject into chunked data?

I have a local HTTP proxy which I use to inject scripts into the HTML header retrieved from remote servers. Simple enough with a standard HTTP stream, but when the transfer is chunked encoding there's a bit more parsing to be done.

The method I have in mind seems somewhat convoluted:

  1. Parse the incoming data for <head>
  2. Parse backwards from <head> and find the hex value at the start of the chunk (maybe before a semi-colon).
  3. Add the size of the injected data to the hex value.
  4. Insert the injected script after <head>.
  5. Forward the modified string to the client (browser).

Would this work? (I'm about to knock out the code to find out) Is there a better way?

Upvotes: 0

Views: 96

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596437

That will not work, because there is no guarantee that the complete <head> tag will be inside a single chunk. It could straddle the boundary between chunks. You need to parse the HTTP chunks themselves the correct way, buffering whatever data is inside of them so you can parse that separately. Read inbound chunks until you have buffered a complete <head> tag, then inject your script as needed, output a new chunk containing your buffered data and the script, and then pass along any subsequent chunks as-is.

Upvotes: 2

Related Questions