Reputation: 106
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:
<head>
<head>
and find the hex value at the start of the chunk (maybe before a semi-colon).<head>
.Would this work? (I'm about to knock out the code to find out) Is there a better way?
Upvotes: 0
Views: 96
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