1 module main; 2 3 import archttp.HttpRequestParser; 4 import archttp.Router; 5 import archttp.HttpRequest; 6 7 import std.stdio; 8 import std.conv : to; 9 import std.file : readText; 10 11 void parseTest0() 12 { 13 string data = `POST /login?action=check HTTP/1.1 14 Host: localhost:8080 15 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0 16 Accept: */* 17 Accept-Language: zh-CN,en-US;q=0.7,en;q=0.3 18 Accept-Encoding: gzip, deflate, br 19 Content-Type: multipart/form-data; boundary=---------------------------332604924416206460511787781889 20 Content-Length: 697 21 Connection: keep-alive 22 Sec-Fetch-Dest: empty 23 Sec-Fetch-Mode: cors 24 Sec-Fetch-Site: same-origin 25 26 `; 27 data ~= "-----------------------------332604924416206460511787781889\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nzoujiaqing\r\n-----------------------------332604924416206460511787781889\r\nContent-Disposition: form-data; name=\"avatar\"; filename=\"a.jpg\"\r\nContent-Type: image/jpeg\r\n\r\nthis is a avatar.\r\n-----------------------------332604924416206460511787781889\r\nContent-Disposition: form-data; name=\"file[]\"; filename=\"url.d\"\r\nContent-Type: application/octet-stream\r\n\r\n/* hello world */\r\n-----------------------------332604924416206460511787781889\r\nContent-Disposition: form-data; name=\"file[]\"; filename=\"a.jpg\"\r\nContent-Type: image/jpeg\r\n\r\nthis is a pic.\r\n-----------------------------332604924416206460511787781889--\r\n"; 28 29 // data = readText("./reqeustdata"); 30 31 auto parser = new HttpRequestParser; 32 33 long result = parser.parse(data); 34 35 writeln("data length: ", data.length); 36 writeln("parsed data: ", result); 37 38 auto request = parser.request(); 39 40 parser.reset(); 41 42 OnRequest(request); 43 } 44 45 void parseTest1() 46 { 47 48 string[] buf; 49 buf ~= `P`; 50 buf ~= `OST /login?action=check HTTP/1.1 51 Host: localhost:8080`; 52 buf ~= ` 53 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0 54 Accept: */* 55 Accept-Language: zh-CN,en-US;q=0.7,en;q=0.3 56 Accept-Encoding: gzip, deflate, br 57 Content-Type: multipart/form-data; boundary=---------------------------332604924416206460511787781889 58 Content-Length: 697 59 Connection: keep-alive 60 Sec-Fetch-Dest: empty`; 61 buf ~= ` 62 Sec-Fetch-Mode: cors 63 Sec-Fetch-Site: same-origin 64 65 `; 66 buf ~= "-----------------------------3326049244162064"; 67 buf ~= "60511787781889\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nzouji"; 68 buf ~= "aqing\r\n-----------------------------332604924416206460511787781889\r\nContent-Disposi"; 69 buf ~= "tion: form-data; name=\"avatar\"; filename=\"a.jpg\"\r\nContent-Type: image/jpeg\r\n\r\nth"; 70 buf ~= "is is a avatar.\r\n-----------------------------332604924416206460511787781889\r\nContent-Disposit"; 71 buf ~= "ion: form-data; name=\"file[]\"; filename=\"url.d\"\r\nContent-Type: application/octet-stre"; 72 buf ~= "am\r\n\r\n/* hello world */\r\n-----------------------------332604924416206460511787781"; 73 buf ~= "889\r\nContent-Disposition: form-data; name=\"file[]\"; filename=\"a.jpg\"\r\nContent-T"; 74 buf ~= "ype: image/jpeg\r\n\r\nthis is a pic.\r\n-----------------------------3326"; 75 buf ~= "04924416206460511787781889--\r\n"; 76 77 auto parser = new HttpRequestParser; 78 79 string data = ""; 80 81 foreach ( b ; buf) 82 { 83 data ~= b; 84 ulong result = parser.parse(data); 85 86 if (parser.parserStatus() == ParserStatus.PARTIAL) 87 { 88 continue; 89 } 90 91 if (parser.parserStatus() == ParserStatus.COMPLETED) 92 { 93 auto request = parser.request(); 94 95 parser.reset(); 96 97 OnRequest(request); 98 } 99 100 if (parser.parserStatus() == ParserStatus.FAILED) 101 { 102 writeln("Parsing error!"); 103 break; 104 } 105 } 106 107 writeln("Request end."); 108 } 109 110 void OnRequest(HttpRequest request) 111 { 112 writeln(request.path()); 113 writeln(request.method()); 114 115 writeln("\nHeaders:"); 116 foreach ( name, value ; request.headers() ) 117 { 118 writeln(name, " - ", value); 119 } 120 121 writeln("\nfields:"); 122 writeln(request.fields); 123 124 writeln("\nfiles:"); 125 writeln(request.files); 126 } 127 128 void main() 129 { 130 parseTest1(); 131 }