JaroslavTulach commented on a change in pull request #26: URL: https://github.com/apache/netbeans-html4j/pull/26#discussion_r759984874
########## File path: browser/src/main/java/org/netbeans/html/presenters/browser/SimpleServer.java ########## @@ -0,0 +1,706 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.html.presenters.browser; + +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.net.InetSocketAddress; +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.channels.ClosedByInterruptException; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.TimeZone; +import java.util.TreeMap; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.netbeans.html.boot.spi.Fn; + +final class SimpleServer extends HttpServer<SimpleServer.ReqRes, SimpleServer.ReqRes, Object, SimpleServer.Context> { + private final Map<String, Handler> maps = new TreeMap<>((s1, s2) -> { + if (s1.length() != s2.length()) { + return s2.length() - s1.length(); + } + return s2.compareTo(s1); + }); + private int max; + private int min; + /** + * @GuardedBy("this") + */ + private ServerSocketChannel server; + /** + * @GuardedBy("this") + */ + private Selector connection; + /** + * @GuardedBy("this") + */ + private Thread processor; + private final List<Runnable> pendingActions = new ArrayList<>(); + + private static final Pattern PATTERN_GET = Pattern.compile("(OPTIONS|HEAD|GET|POST|PUT|DELETE) */([^ \\?]*)(\\?[^ ]*)?"); + private static final Pattern PATTERN_HOST = Pattern.compile(".*^Host: *(.*):([0-9]+)$", Pattern.MULTILINE); + private static final Pattern PATTERN_LENGTH = Pattern.compile(".*^Content-Length: ([0-9]+)$", Pattern.MULTILINE); + static final Logger LOG = Logger.getLogger(SimpleServer.class.getName()); + + SimpleServer() { + } + + @Override + void addHttpHandler(Handler h, String path) { + if (!path.startsWith("/")) { + throw new IllegalStateException("Shall start with /: " + path); + } + maps.put(path.substring(1), h); + } + + @Override + void init(int from, int to) throws IOException { + this.connection = Selector.open(); + this.min = from; + this.max = to; + } + + @Override + synchronized void start() throws IOException { + LOG.log(Level.INFO, "Listening for HTTP connections on port {0}", getServer().socket().getLocalPort()); + processor = new Thread(this::mainLoop, "HTTP server"); + processor.start(); + } + + private final synchronized Thread getProcessorThread() { + return processor; + } + + final void assertThread() { + assert Thread.currentThread() == getProcessorThread(); + } + + @Override + String getRequestURI(ReqRes r) { + assertThread(); + return "/" + r.url; + } + + @Override + String getServerName(ReqRes r) { + assertThread(); + return r.hostName; + } + + @Override + int getServerPort(ReqRes r) { + assertThread(); + return r.hostPort; + } + + @Override + String getParameter(ReqRes r, String id) { + assertThread(); + return (String) r.args.get(id); + } + + @Override + String getMethod(ReqRes r) { + assertThread(); + return r.method; + } + + @Override + String getBody(ReqRes r) { + assertThread(); + if (r.body == null) { + return ""; + } else { + return new String(r.body.array(), StandardCharsets.UTF_8); + } + } + + static int endOfHeader(String header) { Review comment: Thanks. Removed in 432eaf83bc08 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
