Hi axs,

+1 for request. It will help you massively on the client side. I tried to 
do what you're doing a few weeks ago so I could use SSH in cafes. I found 
that I had to do write a blank string into the http stream on the client 
side to force request to send the request headers though. However, I think 
request's 'data' event might give you a String, not a Buffer. This means 
that any binary data might get corrupted. You can get round that by making 
a base64 stream encoder and decoder, but they have to pass binary data 
around as Buffer objects and not strings - stringstream didn't work for me.

Another problem you might get is that some firewalls might make sure that 
your full request is sent to the server before any response is delivered, 
which would prevent streaming. For instance, vodafone's 3g content filter 
will block that implementation. Here is my solution:

https://github.com/johncant/node-http-tunnel

The client polls the server 5 times a second, and they relay all the Stream 
events to eachother, keeping track of the streams. Gets through most 
unhardcore firewalls. Unfortunately it still gets blocked 
by McAfee's firewall, so I can't use it in my local library :(

In the end I just gave up and worked from somewhere devoid of technophobic 
luddites :)

John

On Monday, July 30, 2012 10:18:38 PM UTC+1, axs wrote:
>
> I'm making an http proxy that tunnels data at the transport layer. Here is 
> the complete code:
>
> var http = require('http');
> var net = require('net');
> var url = require('url');
>
> var proxy = http.createServer();
>
> // proxy an HTTP request
> proxy.on('request', function(req, res){
> var uri = url.parse(req.url);
>  var httpMessage = req.method + ' ' + uri.path + ' HTTP/'+ 
> req.httpVersion + '\r\n';
> for(var header in req.headers){
> httpMessage += header + ': ' + req.headers[header] + '\r\n';
> }
> httpMessage += '\r\n';
> req.on('data', function(data){
> httpMessage += data;
> });
> req.on('end', function(data){
> httpMessage += data;
> var client = net.connect(uri.port || 80, uri.hostname, function(){
> client.write(httpMessage);
> client.pipe(req.connection);
> req.connection.pipe(client);
> });
> });
> });
>
> // proxy an HTTPS request
> proxy.on('connect', function(req, socket, head) {
>   var uri = req.url.split(':')
> var tunnel = net.connect({
> port: uri[1],
> host: uri[0]
> }, function() {
> socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
> tunnel.write(head);
> tunnel.pipe(socket);
> socket.pipe(tunnel);
> });
> });
>
> proxy.listen(8080);
>
> My question has to do with http proxy section: I haven't run into this 
> problem yet, but I can see that for potentially large HTTP bodies (such as 
> file transfers), httpMessage may dramatically increase in size. What is the 
> best way to pipe this data over the client tcp socket as it comes in, 
> instead of caching the whole thing and sending all at once? Would be even 
> better if I could just pipe all the parts of the HTTP message (request 
> line, headers, body, ...) as they come into the server.
>
> Thank for any help in advance.
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to