private static Dictionary<string, string> GetHeadersDictionaryFrom(WebHeaderCollection headers) { string pattern = "\\s+"; Dictionary<string, string> dictionary = new Dictionary<string, string>(); for (int i = 0; i < headers.Keys.Count; i++) { string key = headers.GetKey(i).Replace("-", " ").ToUpper(); key = Regex.Replace(key, pattern, ""); string value = headers.Get(headers.GetKey(i)); //System.Console.WriteLine("Key => " + key + " Value => " + value); dictionary.Add(key, value); } return dictionary; }
private string HeadersToString(WebHeaderCollection Headers) { string Output = string.Empty; for (int i = 0; i < Headers.Count; i++) { Output += Headers.GetKey(i) + ": " + Headers[i] + "\r\n"; } return Output; }
public Dictionary<string, string> MapHeaders(WebHeaderCollection headerCollection) { var headers = new Dictionary<string, string>(); for (var i = 0; i < headerCollection.Count; i++) { headers.Add(headerCollection.GetKey(i), string.Join(",", headerCollection.GetValues(i))); } return headers; }
public string GetHeadersString(WebHeaderCollection header = null) { if (header == null) { header = _header; } if (header.IsNullOrEmpty()) { return(string.Empty); } var sb = new StringBuilder(); sb.AppendFormat(HeaderFormat, _header.GetKey(0), _header.Get(0)); for (int i = 1; i < _header.Count; i++) { sb.Append(Environment.NewLine) .AppendFormat(HeaderFormat, _header.GetKey(i), _header.Get(i)); } return(sb.ToString()); }
public void AddHeaders(WebHeaderCollection headers) { if (headers != null) { Headers = new List<Header>(); for (int i = 0; i < headers.Count; ++i) { string header = headers.GetKey(i); foreach (string value in headers.GetValues(i)) { Headers.Add(new Header(header, value)); } } } }
public static void Test_WebHeader_01() { WebHeaderCollection headers = new WebHeaderCollection(); headers.Add("xxx", "yyy"); headers.Add("xxx", "yyy222"); headers.Add("zzz", "fff"); headers.Add("xxx", "ttt"); for (int i = 0; i < headers.Count; ++i) { string key = headers.GetKey(i); foreach (string value in headers.GetValues(i)) { Trace.WriteLine("{0}: {1}", key, value); } } }
private static string FormatHeaders(WebHeaderCollection headers) { var sb = new StringBuilder(); for (int i = 0; i < headers.Count; i++) { string key = headers.GetKey(i); string[] values = headers.GetValues(i); for (int j = 0; j < values.Length; j++) { sb.Append(key).Append(": ").Append(values[j]).Append("\r\n"); } } return(sb.Append("\r\n").ToString()); }
static string FormatHeaders(WebHeaderCollection headers) { var sb = new StringBuilder(); for (int i = 0; i < headers.Count; i++) { string key = headers.GetKey(i); if (WebHeaderCollection.AllowMultiValues(key)) { foreach (string v in headers.GetValues(i)) { sb.Append(key).Append(": ").Append(v).Append("\r\n"); } } else { sb.Append(key).Append(": ").Append(headers.Get(i)).Append("\r\n"); } } return(sb.Append("\r\n").ToString()); }
private static string CanonicalizedUCloudHeaders(WebHeaderCollection header) { string canoncializedUCloudHeaders = string.Empty; SortedDictionary<string, string> headerMap = new SortedDictionary<string, string> (); for (int i = 0; i < header.Count; ++i) { string headerKey = header.GetKey (i); if (headerKey.ToLower().StartsWith ("x-ucloud-")) { foreach (string value in header.GetValues(i)) { if (headerMap.ContainsKey (headerKey)) { headerMap [headerKey] += value; headerMap [headerKey] += @","; } else { headerMap.Add (headerKey, value); } } } } foreach (KeyValuePair<string, string> item in headerMap) { canoncializedUCloudHeaders += (item.Key + @":" + item.Value + @"\n"); } return canoncializedUCloudHeaders; }
private static IEnumerable<KeyValuePair<string, IEnumerable<string>>> ConvertHeaders( WebHeaderCollection webHeaders) { for (var i = 0; i < webHeaders.Count; i++) { var key = webHeaders.GetKey(i); yield return new KeyValuePair<string, IEnumerable<string>>(key, webHeaders.GetValues(i)); } }
static string FormatHeaders (WebHeaderCollection headers) { var sb = new StringBuilder(); for (int i = 0; i < headers.Count ; i++) { string key = headers.GetKey (i); if (WebHeaderCollection.AllowMultiValues (key)) { foreach (string v in headers.GetValues (i)) { sb.Append (key).Append (": ").Append (v).Append ("\r\n"); } } else { sb.Append (key).Append (": ").Append (headers.Get (i)).Append ("\r\n"); } } return sb.Append("\r\n").ToString(); }
/// <summary> /// Handle response headers /// </summary> private void HandleHeader(string url, WebHeaderCollection headers) { for (int i = 0; i < headers.Count; i++) { string Key = headers.GetKey(i); string Value = headers.Get(i); switch (Key) { case "Set-Cookie": HandleResponseSetCookie(url, Value); break; case "Location": this.RedirectLocation = Value; break; } } }
public static string HTTPFetch(string url, string method, WebHeaderCollection headers, byte[] streamBytes, int contentLength, out string[] responseFields, string contentType = "application/x-www-form-urlencoded", string requestAccept = null, string host = null) { _active = true; try { var uri = new Uri(url); if (EndpointCallback != null) EndpointCallback(uri.AbsolutePath); if (string.IsNullOrEmpty(host)) host = uri.Host; string reqStr = String.Format("{0} {1} HTTP/1.1\r\n", method, url) + String.Format("Host: {0}\r\n", host) + String.Format("Connection: Close\r\n"); if (!IsNullOrWhiteSpace(requestAccept)) reqStr += String.Format("Accept: {0}\r\n", requestAccept); if (contentType != null) reqStr += String.Format("Content-Type: {0}\r\n", contentType); if (headers != null) { for (int i = 0; i < headers.Count; ++i) { string header = headers.GetKey(i); foreach (string value in headers.GetValues(i)) { reqStr += String.Format("{0}: {1}\r\n", header, value); } } } reqStr += String.Format("Content-Length: {0}\r\n", contentLength); reqStr += "\r\n"; byte[] headerBytes = ToByteArray(reqStr); byte[] finalBytes = headerBytes; if (contentLength > 0) { var requestBytes = new byte[headerBytes.Length + contentLength]; Buffer.BlockCopy(headerBytes, 0, requestBytes, 0, headerBytes.Length); Buffer.BlockCopy(streamBytes, 0, requestBytes, headerBytes.Length, contentLength); finalBytes = requestBytes; } string responseFromServer = ""; responseFields = new string[] { }; var tcpClient = new TcpClient(); string responseStr = ""; int responseLength = 0; if (url.ToLower().StartsWith("https")) { //HTTPS tcpClient.Connect(uri.Host, 443); ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => true); //This has a bug on mono: Message "The authentication or decryption has failed." //Therefore unfortunately we have to ignore certificates. using (var s = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => true))) { s.AuthenticateAsClient(uri.Host, null, SslProtocols.Tls, false); if (UpstreamCallback != null && UpstreamCallbackRate > 0) { int i = 0; while (i < finalBytes.Length) { if (!_active) throw new Exception("Halt"); if (i + _upstreamCallbackRate > finalBytes.Length) { s.Write(finalBytes, i, finalBytes.Length - i); UpstreamCallback(contentLength, contentLength); break; } s.Write(finalBytes, i, (int)_upstreamCallbackRate); i += (int)_upstreamCallbackRate; UpstreamCallback(Math.Min(i, contentLength), contentLength); } } else s.Write(finalBytes, 0, finalBytes.Length); s.Flush(); while (true) { var responseBytes = new byte[8192]; int i = s.Read(responseBytes, 0, responseBytes.Length); if (i == 0) break; responseStr += Encoding.UTF8.GetString(responseBytes, 0, i); responseLength += i; } } } else { //HTTP tcpClient.Connect(uri.Host, 80); if (UpstreamCallback != null && UpstreamCallbackRate > 0) { var s = tcpClient.GetStream(); int i = 0; while (i < finalBytes.Length) { if (!_active) throw new Exception("Halt"); if (i + _upstreamCallbackRate > finalBytes.Length) { s.Write(finalBytes, i, finalBytes.Length - i); UpstreamCallback(contentLength, contentLength); break; } s.Write(finalBytes, i, (int)_upstreamCallbackRate); i += (int)_upstreamCallbackRate; UpstreamCallback(Math.Min(i, contentLength), contentLength); } } else tcpClient.Client.Send(finalBytes); while (true) { var responseBytes = new byte[8192]; int i = tcpClient.Client.Receive(responseBytes); if (i == 0) break; responseStr += Encoding.UTF8.GetString(responseBytes, 0, i); responseLength += i; } } tcpClient.Close(); var bodyPos = responseStr.IndexOf("\r\n\r\n"); if (bodyPos >= 0) { responseFields = responseStr.Substring(0, bodyPos).Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); bodyPos += 4; responseFromServer = responseStr.Substring(bodyPos, responseStr.Length - bodyPos); } Debug.WriteLine(String.Format("Response from URL {0}:", url), "HTTPFetch"); Debug.WriteLine(responseFromServer, "HTTPFetch"); if (VerboseCallback != null) { VerboseCallback(String.Format("Response from URL {0}:", url)); VerboseCallback(responseFromServer); } return responseFromServer; } catch (Exception e) { _active = false; throw e; } }
public static void Test_WebHeader_02() { string file = @"http\header_01.json"; string file2 = @"http\header_02.json"; file = zPath.Combine(_directory, file); file2 = zPath.Combine(_directory, file2); WebHeaderCollection headers = new WebHeaderCollection(); headers.Add("xxx", "yyy"); headers.Add("xxx", "yyy222"); headers.Add("zzz", "fff"); headers.Add("yyy", "ttt"); for (int i = 0; i < headers.Count; ++i) { string key = headers.GetKey(i); foreach (string value in headers.GetValues(i)) { Trace.WriteLine("{0}: {1}", key, value); } } //headers.zSave(file); BsonDocument doc = headers.ToBsonDocument(); doc.zSave(file); }
/// <summary> /// Fills the Response Header Collection. /// </summary> /// <param name="resp"> The ResponseBuffer type.</param> /// <param name="request"> The HttpWebRequest type.</param> /// <param name="responseHeaders"> The Response WebHeaderCollection.</param> /// <param name="hwr"> The HttpWebResponse type.</param> /// <returns> An updated ResponseBuffer type containing the change.</returns> public static ResponseBuffer FillResponseHeader(ResponseBuffer resp,HttpWebRequest request, WebHeaderCollection responseHeaders, HttpWebResponse hwr) { Hashtable coll = new Hashtable(); coll.Add("Character Set",hwr.CharacterSet); coll.Add("Content Encoding",hwr.ContentEncoding); coll.Add("Last Modified",hwr.LastModified); coll.Add("Method",hwr.Method); coll.Add("Protocol Version",hwr.ProtocolVersion); coll.Add("Response Uri",hwr.ResponseUri); coll.Add("Server",hwr.Server); coll.Add("Status Code",hwr.StatusCode); coll.Add("Status Description",hwr.StatusDescription); coll.Add("Referer", request.Referer); for (int i = 0;i<responseHeaders.Count;i++) { if (!coll.ContainsKey(responseHeaders.GetKey(i)) ) { coll.Add(responseHeaders.GetKey(i), responseHeaders[i]); } } StringBuilder sb = new StringBuilder(); sb.Append("---------------------"); sb.Append("=RESPONSE HEADERS="); sb.Append("---------------------\r\n"); foreach (DictionaryEntry de in coll) { sb.Append(de.Key); sb.Append(":"); sb.Append(de.Value); sb.Append("\r\n"); } resp.ResponseHeader = sb.ToString(); resp.ResponseHeaderCollection = coll; return resp; }
private static string GetCookieString(WebHeaderCollection webHeaderCollection) { try { string CokString = ""; for (int i = 0; i < webHeaderCollection.Count; i++) { String header__ = webHeaderCollection.GetKey(i); if (header__ != "Set-Cookie") continue; String[] values = webHeaderCollection.GetValues(header__); if (values.Length < 1) continue; foreach (string v in values) if (v != "") CokString += MrHTTP.StripCokNameANdVal(v)+";"; } if (CokString.Length > 0) return CokString.Substring(0, CokString.Length - 1); return CokString; } catch { return ""; } }
/// <summary> /// Receives the continue header from delegate and sets it to ContinueHeader property. /// </summary> /// <param name="statusCode"> The status code</param> /// <param name="header"> The header collection.</param> protected void GetRedirectHeaders(int statusCode, WebHeaderCollection header) { StringBuilder textStream = new StringBuilder(); for (int i = 0;i<header.Count;i++) { textStream.Append(header.GetKey(i)); textStream.Append(": "); textStream.Append(header.Get(i)); textStream.Append("\r\n"); } ContinueHeader= textStream.ToString(); }
public void ResetCookie(WebHeaderCollection whc, string host) { for (int i = 0; i < whc.Count; i++) { string key = whc.GetKey(i); string value = whc.Get(i); if (key == "Set-Cookie") { Match match = Regex.Match(value, "(.+?)=(.+?);"); if (match.Captures.Count > 0) { cookieContainer.Add(new Cookie(match.Groups[1].Value, match.Groups[2].Value, "/", host.Split(':')[0])); } } } }
private void ThreadLoop() { while (true) { if (Socket != null) Socket.Close(); // securité: on ferme l'eventuelle Socket précédente try { Socket = tcpListener.AcceptSocket(); } catch (SocketException e) { if (e.SocketErrorCode == SocketError.Interrupted) return; // arrêt du serveur throw; } if (ValidateRemoteAddr((Socket.RemoteEndPoint as IPEndPoint).Address)) try { NetworkStream networkStream = new NetworkStream(Socket, true); networkStream.ReadTimeout = 1000; // timeout if read doesn't succeed after 1 sec using (RequestContent = new StreamReader(networkStream, Encoding.Default)) { string line = null; int index; ProtocolVersion = HttpVersion.Version10; HttpStatusCode StatusCode;// = HttpStatusCode.OK; ErrorDescription = null; Url = null; Query = null; QueryArgs = null; LastModified = DateTime.Now; ResponseHeaders = new WebHeaderCollection(); ContentType = new ContentType("text/html"); ContentLength = -1; Content = null; try { try { line = RequestContent.ReadLine(); } catch (IOException e) { throw new ApplicationException(e.Message, e); } Console.WriteLine("HTTP< " + line); if (line == null) continue; index = line.IndexOf(' '); HttpMethod = line.Substring(0, index); line = line.Substring(index + 1); index = line.IndexOf(' '); if (index >= 0) { string protocolVersion = line.Substring(index + 1); Match m = (new Regex("HTTP/(\\d+).(\\d+)")).Match(protocolVersion); if (!m.Success) throw new Exception("Protocole HTTP invalide"); line = line.Substring(0, index); ProtocolVersion = new Version(Convert.ToInt32(m.Groups[1].Value), Convert.ToInt32(m.Groups[2].Value)); } else ProtocolVersion = HttpVersion.Version10; if (line.Length == 0) throw new Exception("URI manquante"); index = line.IndexOf('?'); if (index >= 0) { Query = line.Substring(index); QueryArgs = System.Web.HttpUtility.ParseQueryString(System.Web.HttpUtility.HtmlDecode(line.Substring(index + 1))); line = line.Substring(0, index); } else { Query = String.Empty; QueryArgs = new NameValueCollection(); } Url = line; Accept = null; Connection = null; RequestContentLength = -1; RequestContentType = null; Host = null; IfModifiedSince = new DateTime(); // DateTime vide Referer = null; UserAgent = null; RequestHeaders = new WebHeaderCollection(); while ((line = RequestContent.ReadLine()).Length != 0) { index = line.IndexOf(':'); if (index < 0) { Console.WriteLine("BasicHttpServer: Header HTTP invalide: " + line); continue; } string headerName = line.Substring(0, index); line = line.Substring(index + 1).Trim(); switch (headerName.ToLower()) { case "accept": Accept = line; break; case "connection": Connection = line; break; case "content-length": RequestContentLength = Convert.ToInt64(line); break; case "content-type": RequestContentType = new ContentType(line); break; case "host": Host = line; break; case "if-modified-since": IfModifiedSince = DateTime.Parse(line); break; case "referer": Referer = line; Console.WriteLine("HTTP< Referer: " + line); break; case "user-agent": UserAgent = line; break; default: Console.WriteLine("BasicHttpServer: Header inconnu: {0}: {1}", headerName, line); RequestHeaders.Add(headerName, line); break; } } try { // handle the request (this function is eventually overriden) StatusCode = HandleRequest(); if (StatusCode != HttpStatusCode.OK) Console.WriteLine("BasicHttpServer: {0} {1}: {2}\r\n\t{3}", (int) StatusCode, StatusCode, Url, ErrorDescription); } #if DEBUG catch (ApplicationException e) #else catch (Exception e) // en mode RELEASE, catch toutes les exceptions pour ne pas planter le serveur #endif { StatusCode = HttpStatusCode.InternalServerError; ErrorDescription = e.Message; Console.WriteLine("BasicHttpServer: {0}:\r\n\tsur la requete {1}{2}", e, Url, Query); } } #if DEBUG catch (ApplicationException e) #else catch (Exception e) // en mode RELEASE, catch toutes les exceptions pour ne pas planter le serveur #endif { StatusCode = HttpStatusCode.BadRequest; ErrorDescription = e.Message; Console.WriteLine("BasicHttpServer: {0}:\r\nsur le parsing de {1}", e, line); } if ((Content == null) && (StatusCode != HttpStatusCode.OK)) { ContentType = new ContentType("text/html"); ReplyFormattedFile(ErrorFile, (int)StatusCode, StatusCode, Url, ErrorDescription.Replace("\r\n", "<br>")); } if (HackStatusCodeAlwaysOK) // cas particulier des clients HTTP qui n'aiment pas autre chose qu'OK (ex: Freebox ...) StatusCode = HttpStatusCode.OK; // envoi des Headers de la réponse StreamWriter writer = new StreamWriter(networkStream, Encoding.Default); writer.WriteLine("HTTP/{0}.{1} {2} {3}", ProtocolVersion.Major, ProtocolVersion.Minor, (int)StatusCode, StatusCode); writer.WriteLine("Last-Modified: " + LastModified.ToString("R")); writer.WriteLine("Location: " + ResponseUri); if (ProtocolVersion > HttpVersion.Version10) { writer.WriteLine("Connection: close"); // c'est un serveur vraiment basique... pas de Keep-Alive for (index = 0; index < ResponseHeaders.Count; index++) writer.WriteLine("{0}: {1}", ResponseHeaders.GetKey(index), ResponseHeaders.Get(index)); } if (Content != null) { writer.WriteLine("Content-Type: " + ContentType); //Console.WriteLine("HTTP> Content-Type: " + ContentType); if ((ContentLength == -1) && Content.CanSeek) ContentLength = Content.Length; if (ContentLength != -1) { writer.WriteLine("Content-Length: " + ContentLength); //Console.WriteLine("HTTP> Content-Length: " + ContentLength); } } writer.WriteLine(); // termine les headers avec une ligne vide try { writer.Flush(); } catch (IOException) { } if (Content != null) { if (HttpMethod != "HEAD") try { // TODO: faire un choix sur le comportement si la position n'est pas au debut du Stream) // actuellement, pour MemoryStream, on balance tout depuis le debut, meme si la position n'est pas au debut // et pour les autres Stream, on balance à partir de la position en cours // (ce qui permet de n'envoyer qu'une portion précise si on veut) if ((Content is MemoryStream) && (ContentLength == Content.Length)) { // variante optimisée pour les MemoryStream ((MemoryStream)Content).WriteTo(networkStream); } else if (ContentLength == -1) { byte[] buffer = new byte[4096L]; do { index = Content.Read(buffer, 0, 4096); networkStream.Write(buffer, 0, index); } while (index != 0); } else { byte[] buffer = new byte[Math.Min(ContentLength, 4096L)]; while (ContentLength > 0) { index = Content.Read(buffer, 0, buffer.Length); if (index == 0) { Console.WriteLine("BasicHttpServer: Supplied Content stream did not contain expected ContentLength"); break; } networkStream.Write(buffer, 0, index); ContentLength -= index; } } } catch (IOException) { } Content.Close(); } // if (Content != null) } // using (content) => Terminate the connection HandleAfterReply(); // opportunité de faire un traitement après que la réponse aie été envoyée } #if DEBUG catch (ApplicationException e) #else catch (Exception e) // en mode RELEASE, catch toutes les exceptions pour ne pas planter le serveur #endif { Console.WriteLine("BasicHttpServer: " + e); } } // while }
/// <summary> /// Fills The Request Header Collection. /// </summary> /// <param name="resp"> The ResponseBuffer type.</param> /// <param name="h"> The WebHeaderCollection type.</param> /// <param name="hwrq"> The HttpWebRequest type.</param> /// <returns> An updated ResponseBuffer type containing the change.</returns> public static ResponseBuffer FillRequestHeader(ResponseBuffer resp,WebHeaderCollection h,HttpWebRequest hwrq) { Hashtable coll = new Hashtable(); coll.Add("Accept",hwrq.Accept); coll.Add("User Agent",hwrq.UserAgent); coll.Add("ContentType",hwrq.ContentType); coll.Add("Method",hwrq.Method); coll.Add("Pipelined",hwrq.Pipelined); coll.Add("Keep Alive",hwrq.KeepAlive); coll.Add("Request Uri",hwrq.RequestUri); coll.Add("Send Chunked",hwrq.SendChunked); coll.Add("Transfer Encoding",hwrq.TransferEncoding); for (int i = 0;i<h.Count;i++) { if ( !coll.ContainsKey(h.GetKey(i)) ) { coll.Add(h.GetKey(i),h[i]); } } StringBuilder sb = new StringBuilder(); sb.Append("---------------------"); sb.Append("=REQUEST HEADERS="); sb.Append("---------------------\r\n"); foreach (DictionaryEntry de in coll) { sb.Append(de.Key); sb.Append(":"); sb.Append(de.Value); sb.Append("\r\n"); } resp.RequestHeaderCollection = coll; resp.RequestHeader = sb.ToString(); return resp; }