-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from 2gis/refactoring-reading-request
Refactoring reading the request in driver
- Loading branch information
Showing
4 changed files
with
87 additions
and
111 deletions.
There are no files selected for viewing
101 changes: 0 additions & 101 deletions
101
WindowsUniversalAppDriver/WindowsUniversalAppDriver/AcceptedRequest.cs
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
WindowsUniversalAppDriver/WindowsUniversalAppDriver/HttpRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
namespace WindowsUniversalAppDriver | ||
{ | ||
#region | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
#endregion | ||
|
||
public class HttpRequest | ||
{ | ||
#region Public Properties | ||
|
||
public Dictionary<string, string> Headers { get; set; } | ||
|
||
public string MessageBody { get; private set; } | ||
|
||
public string StartingLine { get; private set; } | ||
|
||
#endregion | ||
|
||
#region Public Methods and Operators | ||
|
||
public static HttpRequest ReadFromStreamWithoutClosing(Stream stream) | ||
{ | ||
var request = new HttpRequest(); | ||
var streamReader = new StreamReader(stream); | ||
|
||
request.StartingLine = streamReader.ReadLine(); | ||
|
||
request.Headers = ReadHeaders(streamReader); | ||
|
||
var contentLength = GetContentLength(request.Headers); | ||
request.MessageBody = contentLength != 0 ? ReadContent(streamReader, contentLength) : string.Empty; | ||
|
||
return request; | ||
} | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
private static int GetContentLength(IReadOnlyDictionary<string, string> headers) | ||
{ | ||
var contentLength = 0; | ||
string contentLengthString; | ||
if (headers.TryGetValue("Content-Length", out contentLengthString)) | ||
{ | ||
contentLength = Convert.ToInt32(contentLengthString, CultureInfo.InvariantCulture); | ||
} | ||
|
||
return contentLength; | ||
} | ||
|
||
// reads the content of a request depending on its length | ||
private static string ReadContent(TextReader textReader, int contentLength) | ||
{ | ||
var readBuffer = new char[contentLength]; | ||
textReader.Read(readBuffer, 0, readBuffer.Length); | ||
return readBuffer.Aggregate(string.Empty, (current, ch) => current + ch); | ||
} | ||
|
||
private static Dictionary<string, string> ReadHeaders(TextReader textReader) | ||
{ | ||
var headers = new Dictionary<string, string>(); | ||
string header; | ||
while (!string.IsNullOrEmpty(header = textReader.ReadLine())) | ||
{ | ||
var splitHeader = header.Split(':'); | ||
headers.Add(splitHeader[0], splitHeader[1].Trim(' ')); | ||
} | ||
|
||
return headers; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters