Created
December 17, 2021 10:17
-
-
Save hermanbanken/1f98c26e618603efe59b727c72a118d8 to your computer and use it in GitHub Desktop.
java server serving 404 without dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| import java.io.PrintWriter; | |
| import java.net.InetAddress; | |
| import java.net.ServerSocket; | |
| import java.net.Socket; | |
| import java.util.Map; | |
| public class NativeHttpServer { | |
| public static void main(String[] args) throws IOException { | |
| Map<String, String> env = System.getenv(); | |
| String hostname = InetAddress.getLocalHost().getHostName(); | |
| ServerSocket serverSocket = new ServerSocket(Integer.parseInt(env.get("PORT"))); | |
| while (true) { | |
| Socket clientSocket = serverSocket.accept(); | |
| PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); | |
| BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
| String s = in.readLine(); | |
| System.out.println(s); | |
| if (s == "GET /_ah/health HTTP/1.1") { | |
| out.println("HTTP/1.1 200 OK"); | |
| } else { | |
| out.println("HTTP/1.1 404 Not Found"); | |
| } | |
| out.println("Connection: close"); | |
| out.println(); | |
| out.flush(); | |
| clientSocket.close(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment