|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.netty.server; |
| 19 | + |
| 20 | +import static org.openqa.selenium.remote.http.Contents.bytes; |
| 21 | + |
| 22 | +import com.google.common.io.ByteStreams; |
| 23 | + |
| 24 | +import org.openqa.selenium.remote.http.Contents; |
| 25 | +import org.openqa.selenium.remote.http.HttpMethod; |
| 26 | +import org.openqa.selenium.remote.http.HttpRequest; |
| 27 | + |
| 28 | +import io.netty.buffer.ByteBuf; |
| 29 | +import io.netty.buffer.ByteBufInputStream; |
| 30 | +import io.netty.channel.ChannelHandlerContext; |
| 31 | +import io.netty.channel.SimpleChannelInboundHandler; |
| 32 | +import io.netty.handler.codec.http.FullHttpRequest; |
| 33 | +import io.netty.handler.codec.http.HttpContent; |
| 34 | +import io.netty.handler.codec.http.HttpObject; |
| 35 | +import io.netty.handler.codec.http.LastHttpContent; |
| 36 | + |
| 37 | +import java.io.ByteArrayOutputStream; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.InputStream; |
| 40 | +import java.io.PipedInputStream; |
| 41 | +import java.io.PipedOutputStream; |
| 42 | +import java.io.UncheckedIOException; |
| 43 | +import java.util.concurrent.ExecutorService; |
| 44 | +import java.util.concurrent.Executors; |
| 45 | +import java.util.logging.Logger; |
| 46 | + |
| 47 | + |
| 48 | +class RequestConverter extends SimpleChannelInboundHandler<HttpObject> { |
| 49 | + |
| 50 | + private static final Logger LOG = Logger.getLogger(RequestConverter.class.getName()); |
| 51 | + private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor(); |
| 52 | + private PipedOutputStream out; |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void channelRead0( |
| 56 | + ChannelHandlerContext ctx, |
| 57 | + HttpObject msg) throws Exception { |
| 58 | + LOG.fine("Incoming message: " + msg); |
| 59 | + |
| 60 | + if (msg instanceof FullHttpRequest) { |
| 61 | + LOG.fine("Is full http request: " + msg); |
| 62 | + reset(); |
| 63 | + FullHttpRequest nettyRequest = (FullHttpRequest) msg; |
| 64 | + HttpRequest req = createRequest(nettyRequest); |
| 65 | + |
| 66 | + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 67 | + ByteBufInputStream bis = new ByteBufInputStream(nettyRequest.content())) { |
| 68 | + ByteStreams.copy(bis, bos); |
| 69 | + byte[] bytes = bos.toByteArray(); |
| 70 | + req.setContent(bytes(bytes)); |
| 71 | + } |
| 72 | + |
| 73 | + ctx.fireChannelRead(req); |
| 74 | + ctx.flush(); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (msg instanceof io.netty.handler.codec.http.HttpRequest) { |
| 79 | + LOG.fine("Is start of http request: " + msg); |
| 80 | + reset(); |
| 81 | + io.netty.handler.codec.http.HttpRequest nettyRequest = |
| 82 | + (io.netty.handler.codec.http.HttpRequest) msg; |
| 83 | + |
| 84 | + HttpRequest req = new HttpRequest( |
| 85 | + HttpMethod.valueOf(nettyRequest.method().name()), |
| 86 | + nettyRequest.uri()); |
| 87 | + |
| 88 | + nettyRequest.headers().entries().stream() |
| 89 | + .filter(entry -> entry.getKey() != null) |
| 90 | + .forEach(entry -> req.addHeader(entry.getKey(), entry.getValue())); |
| 91 | + |
| 92 | + out = new PipedOutputStream(); |
| 93 | + InputStream in = new PipedInputStream(out); |
| 94 | + |
| 95 | + req.setContent(Contents.memoize(() -> in)); |
| 96 | + ctx.fireChannelRead(req); |
| 97 | + ctx.flush(); |
| 98 | + } |
| 99 | + |
| 100 | + if (msg instanceof HttpContent) { |
| 101 | + ByteBuf buf = ((HttpContent) msg).content().retain(); |
| 102 | + EXECUTOR.submit(() -> { |
| 103 | + try (InputStream inputStream = new ByteBufInputStream(buf)) { |
| 104 | + ByteStreams.copy(inputStream, out); |
| 105 | + } catch (IOException e) { |
| 106 | + throw new UncheckedIOException(e); |
| 107 | + } finally { |
| 108 | + buf.release(); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + if (msg instanceof LastHttpContent) { |
| 114 | + LOG.info("Closing input pipe."); |
| 115 | + EXECUTOR.submit(() -> { |
| 116 | + try { |
| 117 | + out.close(); |
| 118 | + } catch (IOException e) { |
| 119 | + throw new UncheckedIOException(e); |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
| 127 | + ctx.flush(); |
| 128 | + reset(); |
| 129 | + super.channelReadComplete(ctx); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| 134 | + cause.printStackTrace(); |
| 135 | + ctx.close(); |
| 136 | + reset(); |
| 137 | + } |
| 138 | + |
| 139 | + private HttpRequest createRequest(io.netty.handler.codec.http.HttpRequest nettyRequest) { |
| 140 | + HttpRequest req = new HttpRequest( |
| 141 | + HttpMethod.valueOf(nettyRequest.method().name()), |
| 142 | + nettyRequest.uri()); |
| 143 | + |
| 144 | + nettyRequest.headers().entries().stream() |
| 145 | + .filter(entry -> entry.getKey() != null) |
| 146 | + .forEach(entry -> req.addHeader(entry.getKey(), entry.getValue())); |
| 147 | + |
| 148 | + return req; |
| 149 | + } |
| 150 | + |
| 151 | + private void reset() throws Exception { |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | +} |
0 commit comments