1 module hunt.http.server.HttpServerContext; 2 3 import hunt.http.server.HttpServerConnection; 4 import hunt.http.server.HttpServerRequest; 5 import hunt.http.server.HttpServerResponse; 6 7 import hunt.http.HttpOutputStream; 8 9 import hunt.http.HttpMetaData; 10 import hunt.http.HttpStatus; 11 12 import hunt.io.BufferUtils; 13 import hunt.stream.BufferedOutputStream; 14 import hunt.stream.Common; 15 import hunt.Exceptions; 16 import hunt.Functions; 17 import hunt.logging; 18 19 20 /** 21 * 22 */ 23 class HttpServerContext { 24 25 private HttpServerRequest _httpRequest; 26 private HttpServerResponse _httpResponse; 27 private HttpOutputStream _httpOutputStream; 28 private HttpServerConnection _connection; 29 private BufferedOutputStream _bufferedOutputStream; 30 private int _bufferSize = 8 * 1024; 31 32 // private int _status = HttpStatus.OK_200; 33 34 // Action1!ByteBuffer _contentHandler; 35 // Action1!HttpRequest _contentCompleteHandler; 36 // Action1!HttpRequest _messageCompleteHandler; 37 38 this(HttpServerRequest request, HttpServerResponse response, 39 HttpOutputStream outputStream, HttpServerConnection connection) { 40 _httpRequest = request; 41 _httpResponse = response; 42 _httpOutputStream = outputStream; 43 _connection = connection; 44 } 45 46 HttpServerRequest httpRequest() { 47 return _httpRequest; 48 } 49 50 void httpRequest(HttpServerRequest request) { 51 _httpRequest = request; 52 } 53 54 HttpServerResponse httpResponse() { 55 return _httpResponse; 56 } 57 58 void httpResponse(HttpServerResponse response) { 59 _httpResponse = response; 60 _httpOutputStream.setMetaData(response); // binding the response with a httpOutputStream 61 } 62 63 // HttpServerContext onContent(Action1!ByteBuffer handler) { 64 // _contentHandler = handler; 65 // return this; 66 // } 67 68 // HttpServerContext onContentComplete(Action1!HttpServerContext handler) { 69 // _contentCompleteHandler = handler; 70 // return this; 71 // } 72 73 // HttpServerContext onMessageComplete(Action1!HttpServerContext handler) { 74 // _messageCompleteHandler = handler; 75 // return this; 76 // } 77 78 HttpServerConnection connection() { 79 return _connection; 80 } 81 82 int getConnectionId() { 83 return _connection.getId(); 84 } 85 86 OutputStream outputStream() { 87 if (_bufferedOutputStream is null) { 88 _bufferedOutputStream = new BufferedOutputStream(_httpOutputStream, bufferSize); 89 } 90 return _bufferedOutputStream; 91 } 92 93 94 void bufferSize(int size) { _bufferSize = size; } 95 96 int bufferSize() { return _bufferSize; } 97 98 void flush() { 99 if (_bufferedOutputStream !is null) { 100 _bufferedOutputStream.flush(); 101 } 102 } 103 104 bool isCommitted() { 105 return _httpOutputStream !is null && _httpOutputStream.isCommitted(); 106 } 107 108 void write(string value) { 109 write(cast(byte[])value, 0, cast(int)value.length); 110 } 111 112 void write(byte[] buffer, int offset, int len) { 113 try { 114 outputStream().write(buffer, offset, len); 115 } catch (IOException e) { 116 version(HUNT_DEBUG) warning(e.msg); 117 version(HUNT_HTTP_DEBUG) warning(e); 118 // errorf("write data exception " ~ uri.toString(), e); 119 // throw new Exception(e); 120 } 121 } 122 123 void end() { 124 if (_bufferedOutputStream !is null) { 125 _bufferedOutputStream.close(); 126 } 127 else { 128 _httpOutputStream.close(); 129 } 130 } 131 }