1 module hunt.http.codec.http.stream.HttpHandler; 2 3 import hunt.http.HttpOutputStream; 4 import hunt.http.HttpConnection; 5 6 import hunt.http.HttpMetaData; 7 import hunt.http.HttpRequest; 8 import hunt.http.HttpResponse; 9 10 import hunt.Functions; 11 import hunt.util.Common; 12 import hunt.io.ByteBuffer; 13 14 15 /** 16 * 17 */ 18 interface HttpHandler { 19 20 bool content(ByteBuffer item, HttpRequest request, HttpResponse response, 21 HttpOutputStream output, HttpConnection connection); 22 23 bool contentComplete(HttpRequest request, HttpResponse response, 24 HttpOutputStream output, HttpConnection connection); 25 26 bool headerComplete(HttpRequest request, HttpResponse response, 27 HttpOutputStream output, HttpConnection connection); 28 29 bool messageComplete(HttpRequest request, HttpResponse response, 30 HttpOutputStream output, HttpConnection connection); 31 32 void badMessage(int status, string reason, HttpRequest request, 33 HttpResponse response, HttpOutputStream output, HttpConnection connection); 34 35 void earlyEOF(HttpRequest request, HttpResponse response, 36 HttpOutputStream output, HttpConnection connection); 37 } 38 39 /** 40 */ 41 class AbstractHttpHandler : HttpHandler { 42 43 protected Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) _headerComplete; 44 protected Func5!(ByteBuffer, HttpRequest, HttpResponse, HttpOutputStream, 45 HttpConnection, bool) _content; 46 protected Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) _contentComplete; 47 protected Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) _messageComplete; 48 protected Action6!(int, string, HttpRequest, HttpResponse, HttpOutputStream, HttpConnection) _badMessage; 49 protected Action4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection) _earlyEOF; 50 51 override bool headerComplete(HttpRequest request, HttpResponse response, 52 HttpOutputStream output, HttpConnection connection) { 53 if (_headerComplete !is null) { 54 return _headerComplete(request, response, output, connection); 55 } else { 56 return false; 57 } 58 } 59 60 override bool content(ByteBuffer item, HttpRequest request, 61 HttpResponse response, HttpOutputStream output, HttpConnection connection) { 62 if (_content !is null) { 63 return _content(item, request, response, output, connection); 64 } else { 65 return false; 66 } 67 } 68 69 override bool contentComplete(HttpRequest request, HttpResponse response, 70 HttpOutputStream output, HttpConnection connection) { 71 if (_contentComplete !is null) { 72 return _contentComplete(request, response, output, connection); 73 } else { 74 return false; 75 } 76 } 77 78 override bool messageComplete(HttpRequest request, HttpResponse response, 79 HttpOutputStream output, HttpConnection connection) { 80 if (_messageComplete !is null) { 81 return _messageComplete(request, response, output, connection); 82 } else { 83 return true; 84 } 85 } 86 87 override void badMessage(int status, string reason, HttpRequest request, 88 HttpResponse response, HttpOutputStream output, HttpConnection connection) { 89 if (_badMessage !is null) { 90 _badMessage(status, reason, request, response, output, connection); 91 } 92 } 93 94 override void earlyEOF(HttpRequest request, HttpResponse response, 95 HttpOutputStream output, HttpConnection connection) { 96 if (_earlyEOF !is null) { 97 _earlyEOF(request, response, output, connection); 98 } 99 } 100 101 }