Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding local/remote ip and port ServerVariables to env #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/Firefly/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ private void Go(bool newFrame, Frame frame)
{
Services = _services,
App = _app,
Socket = _socket,
Write = _socketSender.Write,
Flush = _socketSender.Flush,
End = ProduceEnd
End = ProduceEnd,
});

if (_baton.Buffer.Count != 0)
Expand Down
18 changes: 18 additions & 0 deletions src/main/Firefly/Http/Frame.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using Firefly.Utils;
Expand All @@ -21,6 +23,7 @@ public struct FrameContext
{
public IFireflyService Services;
public AppDelegate App;
public ISocket Socket;
public Func<ArraySegment<byte>, bool> Write;
public Func<Action, bool> Flush;
public Action<ProduceEndType> End;
Expand Down Expand Up @@ -434,6 +437,21 @@ private IDictionary<string, object> CreateOwinEnvironment()
env["owin.RequestBody"] = (BodyDelegate)_messageBody.Subscribe;
env["owin.RequestScheme"] = "http"; // TODO: pass along information about scheme, cgi headers, etc
env["owin.Version"] = "1.0";

var remoteEndPoint = _context.Socket.RemoteEndPoint as IPEndPoint;
if (remoteEndPoint != null)
{
env["server.REMOTE_ADDR"] = remoteEndPoint.Address.ToString();
env["server.REMOTE_PORT"] = remoteEndPoint.Port.ToString(CultureInfo.InvariantCulture);
}

var localEndPoint = _context.Socket.LocalEndPoint as IPEndPoint;
if (localEndPoint != null)
{
env["server.LOCAL_ADDR"] = localEndPoint.Address.ToString();
env["server.SERVER_PORT"] = localEndPoint.Port.ToString(CultureInfo.InvariantCulture);
}

return env;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/Firefly/Utils/ISocket.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace Firefly.Utils
Expand All @@ -9,6 +10,8 @@ public interface ISocket
bool Blocking { get; set; }
bool NoDelay { get; set; }
bool Connected { get; }
EndPoint LocalEndPoint { get; }
EndPoint RemoteEndPoint { get; }

int Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode);
bool ReceiveAsync(ISocketEvent socketEvent);
Expand Down
17 changes: 17 additions & 0 deletions src/main/Firefly/Utils/SocketWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace Firefly.Utils
Expand Down Expand Up @@ -45,6 +46,22 @@ public bool Connected
}
}

public EndPoint LocalEndPoint
{
get
{
return _socket.LocalEndPoint;
}
}

public EndPoint RemoteEndPoint
{
get
{
return _socket.RemoteEndPoint;
}
}

public int Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
{
return _socket.Receive(buffer, offset, size, socketFlags, out errorCode);
Expand Down
4 changes: 4 additions & 0 deletions src/test/Firefly.Tests/Fakes/FakeSocket.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Firefly.Utils;
Expand Down Expand Up @@ -119,6 +120,9 @@ Action TryReceiveAsync()
public bool NoDelay { get; set; }
public bool Connected { get; private set; }

public EndPoint LocalEndPoint { get; set; }
public EndPoint RemoteEndPoint { get; set; }


public int Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
{
Expand Down
21 changes: 20 additions & 1 deletion src/test/Firefly.Tests/Http/FrameTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using System.Net;
using Xunit;

namespace Firefly.Tests.Http
{
Expand Down Expand Up @@ -116,5 +117,23 @@ public void InsufficientRequestBodyWillWaitForMoreData()
Assert.False(App.RequestBody.Ended);
Assert.Equal("12345", App.RequestBody.Text);
}

[Fact]
public void ValuesForCommonServerVariablesArePresent()
{
Socket.LocalEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80);
Socket.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 12345);
Input.Add("GET /hello/world?x=y HTTP/1.1\r\nHost: london\r\nfoo: bar\r\nfrap: quad\r\n\r\n");
Input.End();
AssertInputState(false, true, "");
AssertOutputState(true);
Assert.Equal(1, App.CallCount);

Assert.Equal("127.0.0.1", App.Env["server.LOCAL_ADDR"]);
Assert.Equal("80", App.Env["server.SERVER_PORT"]);
Assert.Equal("192.168.1.1", App.Env["server.REMOTE_ADDR"]);
Assert.Equal("12345", App.Env["server.REMOTE_PORT"]);
}

}
}
3 changes: 3 additions & 0 deletions src/test/Firefly.Tests/Http/FrameTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class FrameTestsBase
protected readonly FakeApp App;
protected readonly FakeOutput Output;
protected readonly FakeInput Input;
protected readonly FakeSocket Socket;
protected readonly Frame Frame;

public FrameTestsBase()
Expand All @@ -18,11 +19,13 @@ public FrameTestsBase()
App = new FakeApp();
Output = new FakeOutput();
Input = new FakeInput();
Socket = new FakeSocket();
Frame = new Frame(
new FrameContext
{
Services = Services,
App = App.Call,
Socket = Socket,
Write = Output.Write,
Flush = Output.Flush,
End = Output.End,
Expand Down