protected virtual void Parse(T message, System.Net.Http.Headers.HttpHeaders headers, string data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (data.Length == 0) { throw new ArgumentException("data cannot be an empty string.", nameof(data)); } if (!LineTerminators.Any(data.Contains)) { throw new ArgumentException("data is not a valid request, it does not contain any CRLF/LF terminators.", nameof(data)); } using (var retVal = new ByteArrayContent(Array.Empty <byte>())) { var lines = data.Split(LineTerminators, StringSplitOptions.None); // First line is the 'request' line containing http protocol details like method, uri, http version etc. ParseStatusLine(lines[0], message); ParseHeaders(headers, retVal.Headers, lines); } }
private int ParseHeaders(System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders, string[] lines) { // Blank line separates headers from content, so read headers until we find blank line. int lineIndex = 1; string line = null, nextLine = null; while (lineIndex + 1 < lines.Length && !String.IsNullOrEmpty((line = lines[lineIndex++]))) { // If the following line starts with space or tab (or any whitespace), it is really part of this header but split for human readability. // Combine these lines into a single comma separated style header for easier parsing. while (lineIndex < lines.Length && !String.IsNullOrEmpty((nextLine = lines[lineIndex]))) { if (nextLine.Length > 0 && Char.IsWhiteSpace(nextLine[0])) { line += "," + nextLine.TrimStart(); lineIndex++; } else { break; } } ParseHeader(line, headers, contentHeaders); } return(lineIndex); }
/// <summary> /// Parses a line from an HTTP request or response message containing a header name and value pair. /// </summary> /// <param name="line">A string containing the data to be parsed.</param> /// <param name="headers">A reference to a <see cref="System.Net.Http.Headers.HttpHeaders"/> collection to which the parsed header will be added.</param> /// <param name="contentHeaders">A reference to a <see cref="System.Net.Http.Headers.HttpHeaders"/> collection for the message content, to which the parsed header will be added.</param> private void ParseHeader(string line, System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders) { // Header format is // name: value var headerKeySeparatorIndex = line.IndexOf(':', StringComparison.Ordinal); var headerName = line.Substring(0, headerKeySeparatorIndex).Trim(); var headerValue = line.Substring(headerKeySeparatorIndex + 1).Trim(); // Not sure how to determine where request headers and content headers begin, // at least not without a known set of headers (general headers first the content headers) // which seems like a bad way of doing it. So we'll assume if it's a known content header put it there // else use request headers. var values = ParseValues(headerValue); var headersToAddTo = IsContentHeader(headerName) ? contentHeaders : headers; if (values.Count > 1) { headersToAddTo.TryAddWithoutValidation(headerName, values); } else { headersToAddTo.TryAddWithoutValidation(headerName, values[0]); } }
private void FormatHeaders(StringBuilder sb, System.Net.Http.Headers.HttpHeaders headerDictionary) { foreach (var header in headerDictionary) { sb.Append(header.Key); sb.Append(": "); sb.AppendLine(string.Join(", ", header.Value)); } }
public RequestHeaders(System.Net.Http.Headers.HttpHeaders headers) { if (headers == null) { throw new ArgumentNullException(nameof(headers)); } _headers = headers; IsReadOnly = true; }
public static string GetHttpHeaderValue(this System.Net.Http.Headers.HttpHeaders headers, string name) { if (headers != null && headers.TryGetValues(name, out var values) && values != null) { return(string.Join("", values)); } return(null); }
public static StringBuilder AppendHeaders(this StringBuilder builder, SystemHttpHeaders headers) { if (headers is SystemHttpHeaders) { builder.Append(headers.ToString()); } return(builder); }
private bool MatchesHeader(KeyValuePair <string, string> matchHeader, System.Net.Http.Headers.HttpHeaders messageHeader) { if (messageHeader == null) { return(false); } IEnumerable <string> values; if (!messageHeader.TryGetValues(matchHeader.Key, out values)) { return(false); } return(values.Any(v => v == matchHeader.Value)); }
protected virtual HttpContent Parse(T message, System.Net.Http.Headers.HttpHeaders headers, string data) { if (data == null) { throw new ArgumentNullException("data"); } if (data.Length == 0) { throw new ArgumentException("data cannot be an empty string.", "data"); } if (!LineTerminators.Any(data.Contains)) { throw new ArgumentException("data is not a valid request, it does not contain any CRLF/LF terminators.", "data"); } HttpContent retVal = null; try { var contentStream = new System.IO.MemoryStream(); try { retVal = new StreamContent(contentStream); var lines = data.Split(LineTerminators, StringSplitOptions.None); //First line is the 'request' line containing http protocol details like method, uri, http version etc. ParseStatusLine(lines[0], message); int lineIndex = ParseHeaders(headers, retVal.Headers, lines); if (lineIndex < lines.Length - 1) { //Read rest of any remaining data as content. if (lineIndex < lines.Length - 1) { //This is inefficient in multiple ways, but not sure of a good way of correcting. Revisit. var body = System.Text.UTF8Encoding.UTF8.GetBytes(String.Join(null, lines, lineIndex, lines.Length - lineIndex)); contentStream.Write(body, 0, body.Length); contentStream.Seek(0, System.IO.SeekOrigin.Begin); } } } catch { if (contentStream != null) { contentStream.Dispose(); } throw; } } catch { if (retVal != null) { retVal.Dispose(); } throw; } return(retVal); }
protected virtual string SerializeHeader(System.Net.Http.Headers.HttpHeaders headers, string name) { var value = headers?.Contains(name) ?? false?string.Join(",", headers.GetValues(name)) : null; return($"{name}: {value}"); }
public static string sessionResponse(System.Net.Http.Headers.HttpHeaders header, object response) { return(header.ToString() + "\n" + response); }