Add a first cut at a HTTP proxy that slows everything down
This commit is contained in:
parent
53151b1939
commit
85bde0f0c5
|
@ -0,0 +1,22 @@
|
|||
|
||||
GPRBUILD=gprbuild
|
||||
GPRCLEAN=gprclean
|
||||
|
||||
GPR_FILE=dispersion.gpr
|
||||
|
||||
all: prep
|
||||
$(GPRBUILD) -P $(GPR_FILE)
|
||||
|
||||
clean: prep
|
||||
$(GPRCLEAN) -P $(GPR_FILE)
|
||||
|
||||
### Dependency targets
|
||||
|
||||
prep:
|
||||
mkdir -p obj/debug
|
||||
mkdir -p obj/release
|
||||
|
||||
###
|
||||
|
||||
|
||||
.PHONY: all clean prep
|
|
@ -0,0 +1,22 @@
|
|||
with "aws";
|
||||
|
||||
project Dispersion is
|
||||
Version := "1.0.0";
|
||||
|
||||
type Mode_Type is ("debug", "release");
|
||||
Mode : Mode_Type := external ("mode", "debug");
|
||||
|
||||
for Source_Dirs use ("src");
|
||||
for Main use ("dispersion.adb");
|
||||
|
||||
for Object_Dir use "obj/" & Mode;
|
||||
|
||||
package Compiler is
|
||||
case Mode is
|
||||
when "debug" =>
|
||||
for Switches ("Ada") use ("-g", "-gnat05");
|
||||
when "release" =>
|
||||
for Switches ("Ada") use ("-O2", "-gnat05");
|
||||
end case;
|
||||
end Compiler;
|
||||
end Dispersion;
|
|
@ -0,0 +1,59 @@
|
|||
private with Ada.Streams,
|
||||
Ada.Text_IO,
|
||||
AWS.Client,
|
||||
AWS.Headers,
|
||||
AWS.Messages,
|
||||
AWS.Response,
|
||||
AWS.Status;
|
||||
|
||||
package body Callbacks is
|
||||
use Ada.Text_IO;
|
||||
|
||||
function Simple (Request : AWS.Status.Data) return AWS.Response.Data is
|
||||
use AWS.Headers;
|
||||
|
||||
Headers : constant AWS.Headers.List := AWS.Status.Header (Request);
|
||||
begin
|
||||
|
||||
if Headers = AWS.Headers.Empty_List then
|
||||
raise Invalid_Request;
|
||||
end if;
|
||||
|
||||
Put_Line ("Method: " & AWS.Status.Method (Request));
|
||||
Put_Line ("URI: " & AWS.Status.URI (Request));
|
||||
|
||||
|
||||
Put_Line ("Delaying 500ms");
|
||||
delay 0.5;
|
||||
|
||||
Request_Upstream : declare
|
||||
use Ada.Streams,
|
||||
AWS.Messages;
|
||||
|
||||
Connection : AWS.Client.HTTP_Connection;
|
||||
Hostname : constant String := AWS.Headers.Get_Values (Headers, "Host");
|
||||
URL : constant String := "http://" & Hostname & AWS.Status.URI (Request);
|
||||
Method : constant AWS.Status.Request_Method := AWS.Status.Method (Request);
|
||||
|
||||
Result : AWS.Response.Data;
|
||||
Message_Body : AWS.Status.Stream_Element_Array (1 .. 10_000);
|
||||
Last : AWS.Status.Stream_Element_Offset;
|
||||
begin
|
||||
Put_Line ("Should proxy " & URL);
|
||||
|
||||
AWS.Client.Create (Connection, URL);
|
||||
|
||||
AWS.Client.Get (Connection, Result);
|
||||
Put_Line ("Status: " & AWS.Messages.Image (AWS.Response.Status_Code (Result)));
|
||||
|
||||
declare
|
||||
Buf : constant String := AWS.Response.Message_Body (Result);
|
||||
begin
|
||||
return AWS.Response.Build (AWS.Response.Content_Type (Result),
|
||||
Buf);
|
||||
end;
|
||||
end Request_Upstream;
|
||||
|
||||
return AWS.Response.Build ("text/html", "Failed to proxy");
|
||||
end Simple;
|
||||
end Callbacks;
|
|
@ -0,0 +1,8 @@
|
|||
with AWS.Response,
|
||||
AWS.Status;
|
||||
|
||||
package Callbacks is
|
||||
function Simple (Request : AWS.Status.Data) return AWS.Response.Data;
|
||||
|
||||
Invalid_Request : exception;
|
||||
end Callbacks;
|
|
@ -0,0 +1,33 @@
|
|||
--
|
||||
-- Main source file for the Dispersion Proxy
|
||||
--
|
||||
|
||||
private with Ada.Text_IO,
|
||||
AWS.Client,
|
||||
AWS.Response,
|
||||
AWS.Server,
|
||||
AWS.Status;
|
||||
|
||||
with Callbacks;
|
||||
|
||||
procedure Dispersion is
|
||||
use Ada.Text_IO;
|
||||
|
||||
Server : AWS.Server.HTTP;
|
||||
|
||||
begin
|
||||
Put_Line ("Starting Dispersion..");
|
||||
|
||||
AWS.Server.Start (Web_Server => Server,
|
||||
Name => "Dispersion Proxy",
|
||||
Max_Connection => 1,
|
||||
Port => 8980,
|
||||
Callback => Callbacks.Simple'Access);
|
||||
|
||||
Put_Line ("Press 'q' to quit..");
|
||||
AWS.Server.Wait (Mode => AWS.Server.Q_Key_Pressed);
|
||||
|
||||
Put_Line ("Shutting down..");
|
||||
AWS.Server.Shutdown (Web_Server => Server);
|
||||
delay 2.0;
|
||||
end Dispersion;
|
Loading…
Reference in New Issue