Skip to content

Commit

Permalink
Merge pull request #14 from 2gis/refactoring-reading-request
Browse files Browse the repository at this point in the history
Refactoring reading the request in driver
  • Loading branch information
NickAb committed Mar 12, 2015
2 parents 9d9a35f + 1d41d0c commit 6adbc87
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 111 deletions.
101 changes: 0 additions & 101 deletions WindowsUniversalAppDriver/WindowsUniversalAppDriver/AcceptedRequest.cs

This file was deleted.

81 changes: 81 additions & 0 deletions WindowsUniversalAppDriver/WindowsUniversalAppDriver/HttpRequest.cs
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
}
}
14 changes: 5 additions & 9 deletions WindowsUniversalAppDriver/WindowsUniversalAppDriver/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ public void StartListening()
// Get a stream object for reading and writing
using (var stream = client.GetStream())
{
var acceptedRequest = new AcceptedRequest();
acceptedRequest.AcceptRequest(stream);
var acceptedRequest = HttpRequest.ReadFromStreamWithoutClosing(stream);
Logger.Debug("ACCEPTED REQUEST {0}", acceptedRequest.StartingLine);

var response = this.HandleRequest(acceptedRequest);

using (var writer = new StreamWriter(stream))
{
try
Expand Down Expand Up @@ -142,12 +141,9 @@ public void StopListening()

#region Methods

private string HandleRequest(AcceptedRequest acceptedRequest)
private string HandleRequest(HttpRequest acceptedRequest)
{
var request = acceptedRequest.Request;
var content = acceptedRequest.Content;

var firstHeaderTokens = request.Split(' ');
var firstHeaderTokens = acceptedRequest.StartingLine.Split(' ');
var method = firstHeaderTokens[0];
var resourcePath = firstHeaderTokens[1];

Expand All @@ -161,7 +157,7 @@ private string HandleRequest(AcceptedRequest acceptedRequest)
}

var commandName = matched.Data.ToString();
var commandToExecute = new Command(commandName, content);
var commandToExecute = new Command(commandName, acceptedRequest.MessageBody);
foreach (string variableName in matched.BoundVariables.Keys)
{
commandToExecute.Parameters[variableName] = matched.BoundVariables[variableName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AcceptedRequest.cs" />
<Compile Include="Automator\Automator.cs" />
<Compile Include="Automator\Capabilities.cs" />
<Compile Include="CommandExecutorDispatchTable.cs" />
Expand Down Expand Up @@ -100,6 +99,7 @@
<Compile Include="EmulatorHelpers\IGesture.cs" />
<Compile Include="EmulatorHelpers\ScrollGesture.cs" />
<Compile Include="EmulatorHelpers\TapGesture.cs" />
<Compile Include="HttpRequest.cs" />
<Compile Include="Listener.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Program.cs" />
Expand Down

0 comments on commit 6adbc87

Please sign in to comment.