1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2005 --
-- AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify --
-- it under the terms of the GNU General Public License as published by --
-- the Free Software Foundation; either version 2 of the License, or (at --
-- your option) any later version. --
-- --
-- This library is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. --
-- --
-- You should have received a copy of the GNU General Public License --
-- along with this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
------------------------------------------------------------------------------
-- ~ MAIN [STD]
with Ada.Text_IO;
with Ada.Exceptions;
with Ada.Streams.Stream_IO;
with AWS.Client;
with AWS.Digest;
with AWS.Server;
with AWS.Status;
with AWS.MIME;
with AWS.OS_Lib;
with AWS.Response;
with AWS.Messages;
with AWS.Utils;
with Get_Free_Port;
with MD5;
procedure Partial is
use Ada;
use Ada.Streams;
use Ada.Text_IO;
use AWS;
function CB (Request : in Status.Data) return Response.Data;
function File_MD5 (Filename : in String) return MD5.Message_Digest;
-- Compute the MD5 signature for Filename
procedure Dump_Response
(Id : in String;
R : in Response.Data;
Status_Only : in Boolean := False);
-- Dump information about the response
task Server is
entry Wait_Start;
entry Stop;
end Server;
HTTP : AWS.Server.HTTP;
Connect : Client.HTTP_Connection;
R : Response.Data;
Port : Natural := 1286;
P_MD5 : MD5.Message_Digest;
D_MD5 : MD5.Message_Digest;
--------
-- CB --
--------
function CB (Request : in Status.Data) return Response.Data is
URI : constant String := Status.URI (Request);
Filename : constant String := URI (URI'First + 1 .. URI'Last);
begin
Put_Line ("URI : " & URI);
return Response.File (MIME.Content_Type (Filename), Filename);
end CB;
-------------------
-- Dump_Response --
-------------------
procedure Dump_Response
(Id : in String;
R : in Response.Data;
Status_Only : in Boolean := False) is
begin
Text_IO.Put
(Id & " code="
& Messages.Status_Code'Image (Response.Status_Code (R)));
if Status_Only then
Text_IO.New_Line;
else
Text_IO.Put_Line
(" length="
& Response.Content_Length_Type'Image
(Response.Content_Length (R)));
end if;
end Dump_Response;
--------------
-- File_MD5 --
--------------
function File_MD5 (Filename : in String) return MD5.Message_Digest is
File : Stream_IO.File_Type;
Buffer : Stream_Element_Array (1 .. 4 * 1_024);
Last : Stream_Element_Offset;
Ctx : MD5.Context;
begin
Stream_IO.Open (File, Stream_IO.In_File, Filename);
loop
Stream_IO.Read (File, Buffer, Last);
MD5.Update (Ctx, Buffer (1 .. Last));
exit when Last < Buffer'Last;
end loop;
Stream_IO.Close (File);
return MD5.Digest (Ctx);
end File_MD5;
------------
-- Server --
------------
task body Server is
begin
Get_Free_Port (Port);
AWS.Server.Start
(HTTP, "Test Partial Download.",
CB'Unrestricted_Access, Port => Port, Max_Connection => 2);
accept Wait_Start;
accept Stop;
exception
when E : others =>
Put_Line ("Server Error " & Exceptions.Exception_Information (E));
end Server;
begin
-- Compute partial.o MD5
P_MD5 := File_MD5 ("partial.o");
Server.Wait_Start;
Client.Create
(Connection => Connect,
Host => "http://localhost:" & Utils.Image (Port));
-- Get partial.o by chunck
declare
use type Client.Content_Bound;
Size : constant Stream_Element_Offset :=
OS_Lib.File_Size ("partial.o");
R1, R2, R3 : Response.Data;
File : Stream_IO.File_Type;
begin
Client.Get (Connect, R1, "/partial.o", (0, 1_023));
Dump_Response ("R1", R1);
Client.Get (Connect, R3, "/partial.o", (Client.Undefined, 1_024));
Dump_Response ("R3", R3);
Client.Get (Connect, R2, "/partial.o",
(1_024, Client.Content_Bound (Size) - 1_024 - 1));
Dump_Response ("R2", R2, Status_Only => True);
Stream_IO.Create (File, Stream_IO.Out_File, "partial.o.downloaded");
Stream_IO.Write (File, Response.Message_Body (R1));
Stream_IO.Write (File, Response.Message_Body (R2));
Stream_IO.Write (File, Response.Message_Body (R3));
Stream_IO.Close (File);
end;
Client.Close (Connect);
Server.Stop;
-- Compute MD5 of downloaded file, and check with original file
D_MD5 := File_MD5 ("partial.o.downloaded");
if P_MD5 = D_MD5 then
Text_IO.Put_Line ("OK partial download");
else
Text_IO.Put_Line ("NOK partial download");
end if;
exception
when E : others =>
Server.Stop;
Put_Line ("Main Error " & Exceptions.Exception_Information (E));
end Partial;
|