public static proxied_http_response Create(http_request req, connection con) { if (req.Header.Values.ContainsKey("Proxy-Connection")) { String pcon = req.Header.GetValue("Proxy-Connection"); req.Header.Values.Remove("Proxy-Connection"); if (String.IsNullOrEmpty(pcon) == false) req.Header.SetValue("Connection", pcon); } //ALL CUSTOM TAGS MUST BE REMOVED BEFORE SENDING HEADERS. //THIS IS FOR ONE TAG, NEEDS TO BE MORE ABSTACT. SUCH AS //REMOVE ALL "X-ANYTHING" TAGS. NEED TO FIND A WAY TO DO //THIS IN THE HEADER LOGIC POSSIBLY! if(req.Header.Values.ContainsKey("X-Profile")) req.Header.Values.Remove("X-Profile"); // Formatting the proper header needs to happen before now // Correcting Target header. if(req.Header.Target.ToLower().StartsWith("http")) { if(req.Uri.OriginalString.Contains(String.Format("{0}:{1}",req.Uri.Host,req.Uri.Port))) { req.Header.Target = req.Uri.OriginalString.Substring( req.Uri.OriginalString.IndexOf(String.Format("{0}:{1}",req.Uri.Host,req.Uri.Port)) + String.Format("{0}:{1}",req.Uri.Host,req.Uri.Port).Length); } else { req.Header.Target = req.Uri.OriginalString.Substring( req.Uri.OriginalString.IndexOf(req.Uri.Host) + req.Uri.Host.Length); } } #if !OFFLINE_DEBUG if(con.data_available) { #if DEBUG byte[] bytes = con.read(); Console.Error.WriteLine(String.Format("{0}: {1}","EARLY SERVER DATA!",Encoding.Default.GetString(bytes))); #endif proxied_retry_exception ex = new proxied_retry_exception(req,"We have not sent a request, yet the server has sent us something! This should not happen ever!",null,502); ex.secure = con.stream is SslStream; throw ex; } ///Send Request if (con.Client.Connected) { if (con.Client.Client.Poll(1, SelectMode.SelectWrite) && !con.Client.Client.Poll(1,SelectMode.SelectError)) { ///req.toByteArray() should be the entire request including POST! byte[] requestbytes = req.ToByteArray(); try { con.stream.Write(requestbytes, 0, requestbytes.Length); con.stream.Flush(); } catch { proxied_retry_exception ex = new proxied_retry_exception(req,"Can not send request upstream!",null,502); ex.secure = con.stream is SslStream; throw ex; } } else { proxied_retry_exception ex = new proxied_retry_exception(req,"Can not send request upstream!",null,502); ex.secure = con.stream is SslStream; throw ex; } } else { proxied_retry_exception ex = new proxied_retry_exception(req,"Upstream server disconnected while send request!",null,502); ex.secure = con.stream is SslStream; throw ex; } //wait for data int count = 0; while (!con.is_disposed && !con.data_available && count < 3000 && con.Client.Client.Connected && req.Connection.Client.Connected && !con.Client.Client.Poll(1, SelectMode.SelectError) && !req.Connection.Client.Poll(1, SelectMode.SelectError)) { //if(server.Client.Poll(100, SelectMode.SelectRead)) // throw new Exception("Server connection failed!"); if(count == 2999) { proxied_retry_exception ex = new proxied_retry_exception(req,"Server failed to respond in a timely manner after sending request!",null,502); ex.secure = con.stream is SslStream; throw ex; } System.Threading.Thread.Sleep(100); count += 1; } #endif proxied_http_response pres = new proxied_http_response(); pres.Request = req; return pres; }
public override void Initialize (TcpClient client, Stream networkStream) { try { base.Initialize (client, networkStream); } catch (Exception ex) { #if DEBUG if(_request != null) Console.Error.WriteLine("Error: " +_request.Uri.ToString()); Console.Error.WriteLine(ex.Message); Console.Error.WriteLine(ex.StackTrace); #endif proxied_retry_exception ex1 = new proxied_retry_exception( this.Request,"Response init problem",ex,502); ex1.secure = networkStream is SslStream; this.Errors.Add(ex1); throw ex1; } if(Header.Statuscode == "408") { proxied_retry_exception ex = new proxied_retry_exception( this.Request,"Server replied with 'bad request'",null,408); ex.secure = networkStream is SslStream; this.Errors.Add(ex); throw ex; } }