/* * * */ public void HandleClientRequest(RequestObj pRequestObj) { String lHTTPRequestString = pRequestObj.ClientStreamReader.ReadLine(); if (String.IsNullOrEmpty(lHTTPRequestString)) { throw new Exception("Client request stream corrupted."); } // Read the request headers from the client and copy them to the request settings // Set values : METHOD scheme://host/Path Logging.LogMessage(String.Format("HTTPClientRequest.HandleClientRequest() : HTTP Request string: \"{0}\"", lHTTPRequestString), Logging.Level.DEBUG, pRequestObj.Counter); pRequestObj.parseRequestString(lHTTPRequestString); this.ReadRequestHeadersFromClient(pRequestObj); pRequestObj.Host = pRequestObj.ClientRequestHeaders["host"].ToString(); pRequestObj.Scheme = "http"; /* * Verify if request parameters are correct. */ if (!Regex.Match(pRequestObj.Method.ToLower(), @"^\s*(get|put|post|head|delete|trace|options|connect)\s*$").Success) { throw new WebException("Client request contains an unsupproted request method"); } String lRequestedURL = String.Format("{0}://{1}{2}", pRequestObj.Scheme, pRequestObj.Host, pRequestObj.Path); /* * If HTTP request was redirected to an other URL in a previous request * replace current request with the redirect location */ if (RedirectCache.Instance.NeedsRequestBeMapped(lRequestedURL)) { Logging.LogMessage(String.Format("HTTPClientRequest.HandleClientRequest() : REDIRECT(301/302) from \"{0}\" to \"{1}\"", lRequestedURL, RedirectCache.Instance.GetElement(lRequestedURL).URL), Logging.Level.INFO, pRequestObj.Counter); // DO SSL STRIPPING THINGS HERE !!!! // Replace the URL by the URL saved in the cache ... RedirectHost lTmpHost = RedirectCache.Instance.GetElement(pRequestObj.getRequestedURL()); pRequestObj.Method = "GET"; pRequestObj.Scheme = lTmpHost.Scheme; pRequestObj.Host = lTmpHost.Host; pRequestObj.Path = lTmpHost.Path; lTmpHost.IncCounter(); } // if (RedirectCac... /* * If HTTP request was answered with an HSTS server response header * conduct an HTTPS request instead of HTTP */ else if (HSTSCache.Instance.GetElement(pRequestObj.Host) != null) { Logging.LogMessage(String.Format("HTTPClientRequest.HandleClientRequest() : HSTS protocol replacement : http://{0} -> https://{0} \n", pRequestObj.Host), Logging.Level.DEBUG, pRequestObj.Counter); pRequestObj.Scheme = "https"; } // if (HSTSCache... try { /* * Forward request according to the URL defined * in the command line parameter. */ if (!String.IsNullOrEmpty(Config.RedirectToURL) && pRequestObj.Method.ToLower() == "get") { HTTPRedirect.getInstance().processRequest(pRequestObj.ClientStream, Config.RedirectToURL, pRequestObj.ClientRequestHeaders); } else { this.DoHttpProcessing(pRequestObj); } } catch (WebException lEx) { /* * 1. Sending server response */ var lResponse = lEx.Response as HttpWebResponse; if (lResponse != null && lResponse.StatusCode != HttpStatusCode.OK) { String ErrorCode = String.Format("HTTP/{0} {1} {2}\n", lResponse.ProtocolVersion, (int)lResponse.StatusCode, lResponse.StatusDescription); pRequestObj.ClientStream.Write(Encoding.ASCII.GetBytes(ErrorCode), 0, ErrorCode.Length); } else { String ErrorCode = String.Format("HTTP/1.1 500 Internal server error\n"); pRequestObj.ClientStream.Write(Encoding.ASCII.GetBytes(ErrorCode), 0, ErrorCode.Length); } // if (lRespon... /* * 2. Sending server response headers */ if (lEx.Response != null && lEx.Response.Headers != null && lEx.Response.Headers.Count > 0) { foreach (String la in lEx.Response.Headers.AllKeys) { String lResp = String.Format("{0}: {1}\n", la, lEx.Response.Headers[la]); pRequestObj.ClientStream.Write(Encoding.ASCII.GetBytes(lResp), 0, lResp.Length); } // foreach (Stri... } // if (lEx.Resp ... pRequestObj.ClientStream.Write(Encoding.ASCII.GetBytes("\n"), 0, 1); /* * 3. Sending server response body */ try { var resp = new StreamReader(lEx.Response.GetResponseStream()).ReadToEnd(); pRequestObj.ClientStream.Write(Encoding.ASCII.GetBytes(resp), 0, resp.Length); } catch (Exception lEx2) { Logging.LogMessage(String.Format("HTTPClientRequest.DoHttpProcessing(EXCEPTION2) : {0} -> {1} \n{2}", pRequestObj.getRequestedURL(), lEx.Message, lEx.StackTrace), Logging.Level.ERROR, pRequestObj.Counter); } } catch (Exception lEx) { Logging.LogMessage(String.Format("HTTPClientRequest.DoHttpProcessing(EXCEPTION) : {0} -> {1} \n{2}", pRequestObj.getRequestedURL(), lEx.Message, lEx.StackTrace), Logging.Level.ERROR, pRequestObj.Counter); } finally { if (pRequestObj.ClientStreamReader != null) { pRequestObj.ClientStreamReader.Close(); } if (pRequestObj.ClientStream != null) { pRequestObj.ClientStream.Close(); } if (pRequestObj.ServerWebResponse != null) { pRequestObj.ServerWebResponse.Close(); } } }
/* * * */ private static void DoHttpProcessing(String pSrcMAC, String pSrcIP, String pSrcPort, TcpClient pClient) { Stream lClientStream = pClient.GetStream(); Stream lOutStream = lClientStream; StreamReader lClientStreamReader = new StreamReader(lClientStream); String[] lReqSplitBuffer; String lMethod = String.Empty; String lRemoteURI = String.Empty; String lURL = String.Empty; Version lVersion = new Version(1, 0); int lContentLen = 0; String lHTTPData = String.Empty; String lHTTPCommand = String.Empty; String lLine = String.Empty; String lPOSTData = String.Empty; Hashtable lHeaders = new Hashtable(); try { // Read the first line HTTP command lHTTPCommand = lClientStreamReader.ReadLine(); /* * Header corrupted. Stop thread execution. */ if (String.IsNullOrEmpty(lHTTPCommand)) { lClientStreamReader.Close(); lClientStream.Close(); return; } /* * Break up the line into three components */ lReqSplitBuffer = lHTTPCommand.Split(lSpaceSplit, 3); lMethod = lReqSplitBuffer[0]; lRemoteURI = lReqSplitBuffer[1]; lURL = String.Empty; // Read the request headers from the client and copy them to our request lContentLen = ReadRequestHeaders(lClientStreamReader, ref lHeaders); // Build the URI if (lHeaders != null && lHeaders.ContainsKey("host") && lHeaders["host"].ToString().Length > 0) { lURL = string.Format("http://{0}:{1}{2}", lHeaders["host"].ToString(), cRemotePort, lRemoteURI); } else { lURL = string.Format("http://{0}:{1}{2}", cRemoteHost, cRemotePort, lRemoteURI); } if (Program.cDebuggingOn) { Console.WriteLine("{0} URL : \"{1}\"", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), lURL); } /* * Redirect URL */ if (!String.IsNullOrEmpty(cRedirectURL) && lMethod.ToLower() == "get") { HTTPRedirect.getInstance().processRequest(lOutStream, cRedirectURL, lHeaders); /* * Process HTTP request */ } else { // StreamReader pClientStreamReader, Stream pOutStream, String pMethod, String pURL, Hashtable pHeaders, int pContentLen, String pHTTPCommand, String pPOSTData HTTPProxy.getInstance().processRequest(lClientStreamReader, lOutStream, lMethod, lURL, lHeaders, lContentLen, lHTTPCommand, out lHTTPData); String lPipeData = String.Format("TCP||{0}||{1}||{2}||{3}||{4}||{5}\r\n", pSrcMAC, pSrcIP, pSrcPort, cRemoteIP, cRemotePort, lHTTPData); Program.WriteToPipe(lPipeData); if (Program.cDebuggingOn) { Console.WriteLine(lPipeData.TrimEnd()); } } // if (!String.IsNullOrEmpty(cR... } catch (Exception ex) { if (Program.cDebuggingOn) { Console.WriteLine(ex.Message); } } finally { if (lClientStreamReader != null) { lClientStreamReader.Close(); } if (lClientStream != null) { lClientStream.Close(); } if (lOutStream != null) { lOutStream.Close(); } } }