titanium-web-proxy icon indicating copy to clipboard operation
titanium-web-proxy copied to clipboard

CONNECT processing

Open obbana2 opened this issue 8 years ago • 18 comments

Good afternoon. I use your proxy for my needs, I change it. Any proxoficator redirecting requests to the HTTPS proxy, wraps HTTP in CONNECT. Titanium does not see this and processes the request as CONNECT, I changed the code if I see that the first part of the body after CONNECT is HTTP, I send a request to the handler as a TRANSPARENT request and it works. It looks sad, but it works.

obbana2 avatar Mar 21 '18 10:03 obbana2

I would also like to have a function to intercept the data without holding BODY - it is desirable to have separate Callback calls. To prevent large files from being loaded into RAM.

obbana2 avatar Mar 21 '18 10:03 obbana2

"To prevent large files from being loaded into RAM.": By default it works in this way, the body only streamed to the client.

Do you call this methods?

await e.GetRequestBody();
or 
await e.GetRequestBodyAsString();

honfika avatar Mar 21 '18 13:03 honfika

Yes, I use these functions. Content-Size is not always present, Content-Type is not always present, and sometimes it is erroneous. content can be chunked. In this case, it would be nice to be able to specify the maximum size of the body that would be clipped when writing to the cache. I'm testing the proxy at a load of about 700,000 http requests per day. Https I can not and do not want to decrypt, but the decryption disabling features also do not? And I do not remember being too lazy to re-read the code, if SetRequestBody and SetRequestBodyAsString are re-compressed, if they were compressed? headers gzip after all remain.

obbana2 avatar Mar 21 '18 17:03 obbana2

I try to get only html and javascript, but it does not always work correctly, I'm looking at the content-type or the end of the .html .js path. In the end, something hangs after about 10-15 minutes of work and I can not understand anything, since there are no exceptions and there is no VisualStudio on the server, and detailed logs would strongly slow down the proxy. In addition, I use the example with WPF as a basis, I probably have to disable the output of sessions on the screen, it turns out that synchronization and it slows down.

obbana2 avatar Mar 21 '18 17:03 obbana2

Still (maybe it seems to me) you should not use await everywhere, somewhere you can send data asynchronously without waiting, just sending the data where to go, because the connection then waits for the reactions? I hope you will not judge an amateur novice.

obbana2 avatar Mar 21 '18 17:03 obbana2

What cache do you mean? We are not caching the body. Unless you set the SessionEventArgs.WebSession.Response.KeepBody property to true, but it is false by default. (WPF example sets it, so if you used that as a sample, them probably you only need to remove that setting.)

GetRequestBody/AsString reads the complete body to the memory, but it is there only until the session is not finished (so basically until the response is sent to the client). It is mandatory, we should store it since it is needed to send it to the client. Unless you replace it with a new body, but in this case it will be unavailable at the moment when you replace it.

Why do you need the body? If you don't call this method, the body will be streamed without loading it to the memory.

Yes, it is compressing the body according to the header value.

honfika avatar Mar 21 '18 17:03 honfika

I insert a small page in the page (on top) about using my proxy and micro advertisement. It happens that the content type html - but the file is binary, it turns out a lot of information gets into RAM, if this file is 5GB, will it crash?

obbana2 avatar Mar 22 '18 03:03 obbana2

So the content type is html and transfer cencoding is chunked? Only this case is the problem, right? Why don't you ignore this? I think it is usually not a html page.

honfika avatar Mar 22 '18 07:03 honfika

found an error in the develop branch, or rather it is an error in the CustomBinaryReader. Incorrect end-of-line processing, which is often wrong. Why could not I use the standard parent methods Stream.ReadLineAsync?

screen

obbana2 avatar Mar 23 '18 11:03 obbana2

Checked, html together with chunked are found often!

obbana2 avatar Mar 23 '18 11:03 obbana2

i replace

//if new line
                if (lastChar == '\r' && newChar == '\n')
                {
                    return encoding.GetString(buffer, 0, bufferDataLength - 1);
                }

on

//if new line
                if (lastChar == '\r' && newChar == '\n')
                {
                    return encoding.GetString(buffer, 0, bufferDataLength - 1);
                }

                //if new line ends with '\n'
                if (newChar == '\n')
                {
                    return encoding.GetString(buffer, 0, bufferDataLength);
                }

in CustomBinaryReader and this worked...

obbana2 avatar Mar 23 '18 11:03 obbana2

Thanks, fixed in the latest beta package. We can't use the standard ReadLineAsync. because the object called stream is not (always) a .NET Stream object.

honfika avatar Mar 23 '18 13:03 honfika

So how can I help you with the chunked problem?

honfika avatar Mar 23 '18 13:03 honfika

the question is settled.

obbana2 avatar Mar 23 '18 14:03 obbana2

The user may want to access the stream directly to save large files to somewhere in file system or database, and then stream it again without loading full file to RAM. So one day soon we can perhaps add a new method to expose those respective streams. Something like

//gets a wrapper stream to server 
//if request is chunked the wrapper stream will parse the chunks
//will throw exception if tried to access after request to server is finished
var serverStream = e.GetRequestBodyStream();
var read = 0;
while ((read = await serverStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
 someUserStream.Write(buffer, 0, read);
}

//gets a wrapper stream to client
//if response is chunked the wrapper stream will encode the chunks
//will throw exception if tried to access this before request to server is finished
var clientStream = e.GetResponseBodyStream();
var read = 0;
while ((read = await  someUserStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
 clientStream.WriteAsync(buffer, 0, read);
}

If the body is already read/send then this should throw exceptions. If the request/response is chunked then we should take care of it inside the methods. The user should be aware of the risks of directly using these streams.

justcoding121 avatar Mar 23 '18 16:03 justcoding121

Something similar is already possible with the DataReceived/DataSent events. It was not called in this case, but some miites ago I added it.

There are 2 problems with this (and it is true for your idea,too):

  • it is read only, so the users can't modify the response (e.g: add an advertisement)
  • when the body is compressed, the events/stream returns the compressed data

honfika avatar Mar 23 '18 20:03 honfika

I think we can compress/decompress inside our wrapper stream using the header values for compression type. We can force chunked when using these new methods by updating the headers, so it wouldn't matter what the content length is. We will document this accordingly.

justcoding121 avatar Mar 23 '18 21:03 justcoding121

Decompression is OK, but there is a problem with the compression. We need to write the ContentLength before streaming the response. And it is unknown. So streaming can work only when no compression (and stream length is known) or with chunked encoding.

honfika avatar Mar 24 '18 19:03 honfika