|
| 1 | +package robaho.net.httpserver; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.net.InetSocketAddress; |
| 7 | +import java.net.URI; |
| 8 | + |
| 9 | +import com.sun.net.httpserver.Headers; |
| 10 | +import com.sun.net.httpserver.HttpContext; |
| 11 | +import com.sun.net.httpserver.HttpExchange; |
| 12 | +import com.sun.net.httpserver.HttpPrincipal; |
| 13 | + |
| 14 | +import robaho.net.httpserver.http2.HTTP2Stream; |
| 15 | + |
| 16 | +public class Http2ExchangeImpl extends HttpExchange { |
| 17 | + private final Headers request; |
| 18 | + private final Headers response; |
| 19 | + private final InputStream in; |
| 20 | + private final OutputStream out; |
| 21 | + private final URI uri; |
| 22 | + private final String method; |
| 23 | + private final HttpContext ctx; |
| 24 | + private final HTTP2Stream stream; |
| 25 | + private HttpPrincipal principal; |
| 26 | + private int responseCode; |
| 27 | + |
| 28 | + public Http2ExchangeImpl(HTTP2Stream stream, URI uri, String method, HttpContext ctx, Headers request, Headers response, InputStream in, OutputStream out) { |
| 29 | + this.request = request; |
| 30 | + this.response = response; |
| 31 | + this.stream = stream; |
| 32 | + this.in = in; |
| 33 | + this.out = out; |
| 34 | + this.uri = uri; |
| 35 | + this.method = method; |
| 36 | + this.ctx = ctx; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Headers getRequestHeaders() { |
| 41 | + return request; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Headers getResponseHeaders() { |
| 46 | + return response; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public InputStream getRequestBody() { |
| 51 | + return in; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public OutputStream getResponseBody() { |
| 56 | + return out; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public URI getRequestURI() { |
| 61 | + return uri; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String getRequestMethod() { |
| 66 | + return method; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public HttpContext getHttpContext() { |
| 71 | + return ctx; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void close() { |
| 76 | + stream.close(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void sendResponseHeaders(int rCode, long responseLength) throws IOException { |
| 81 | + if(responseLength>0) { |
| 82 | + response.set("Content-Length", Long.toString(responseLength)); |
| 83 | + } else if(responseLength==0) { |
| 84 | + // no chunked encoding so just ignore |
| 85 | + } else { |
| 86 | + // -1 means no data will be sent, so should set end of stream |
| 87 | + // response.set("Content-Length", Long.toString(responseLength)); |
| 88 | + } |
| 89 | + response.set(":status",Long.toString(rCode)); |
| 90 | + responseCode = rCode; |
| 91 | + stream.writeResponseHeaders(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public InetSocketAddress getRemoteAddress() { |
| 96 | + return stream.getRemoteAddress(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public InetSocketAddress getLocalAddress() { |
| 101 | + return stream.getLocalAddress(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public int getResponseCode() { |
| 106 | + return responseCode; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String getProtocol() { |
| 111 | + return "HTTP/2"; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public Object getAttribute(String name) { |
| 116 | + return ctx.getAttributes().get(name); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void setAttribute(String name, Object value) { |
| 121 | + ctx.getAttributes().put(name, value); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void setStreams(InputStream i, OutputStream o) { |
| 126 | + throw new UnsupportedOperationException("Not implemented yet"); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public HttpPrincipal getPrincipal() { |
| 131 | + return principal; |
| 132 | + } |
| 133 | +} |
0 commit comments