nsapi plugin chunked encoding
I am coding a nsapi plugin to rewrite urls within html in an output filter. This will allow us to put files to a content delivery network.The html is generated by a vendor application that we are not able to change.
What is does:
If there is a relative url inside an html page to a static file (.gif,.css,.js) then prefix the url with a domain name.
It is complete except that the web server sends the data back in chunks. If a url starts in one chunk and ends in another chunk then the code can't figure out to rewrite that url.
The customization point I am using already has the amount of incoming bytes set.
int rewritefilter_write(FilterLayer *layer, const void *buf, int amount)
I tried to set the following to disable output stream buffering but it had not effect.
Service method="(GET|HEAD|POST)" type="*~magnus-internal/*" fn="send-file" UseOutputStreamSize="0"
Any Ideas?
# 1
Your NSAPI filter sees the data as multiple buffers because the vendor application sends the generated HTML as multiple buffers. (It's best not to call these buffers "chunks". Chunks are defined by HTTP/1.1, and there is not necessarily a direct correlation between these buffers and HTTP/1.1 chunks.)
I see two options:
a) Buffer the data in your filter's write filter method. In your filter's remove filter method, scan the complete response body, rewrite the URLs, and send the rewritten body.
b) Make your search and replace algorithm understand buffer boundaries. The replace.c example that's included with Web Server shows how this can be done.
# 2
I started coding option b. One issue is that the buffer can be anywhere from 0 bytes to 4K.Sometimes I am getting several buffers in a row with only 40 bytes which will make the code more complex if a url spans 3 buffers.
Now I would like to save up buffers in the filter's writer method until there there is several kilobytes of data before calling my replace.
How do you know when the last buffer has come?