class Analyzer::Java::HttpServer

Overview

JDK built-in HTTP server (com.sun.net.httpserver.HttpServer, jdk.httpserver module, available since Java 6). Routes are registered with server.createContext("/path", handler), where the handler is a lambda, an HttpHandler instance/anonymous class, or a method reference.

Unlike declarative frameworks the HTTP verb is not bound at registration — handlers branch on exchange.getRequestMethod() — so this analyzer recovers the path from the createContext call and the verb(s)/params from the resolved handler body. When the handler is a named/anonymous HttpHandler, its handle(...) body is resolved within the same file (cross-file handler classes are out of scope, mirroring the other JVM extractors).

Reference: https://docs.oracle.com/en/java/javase/21/docs/api/jdk.httpserver/com/sun/net/httpserver/HttpServer.html

Defined in:

analyzer/analyzers/java/httpserver.cr

Constant Summary

BODY_VERBS = ["POST", "PUT", "PATCH", "DELETE"]

Verbs that carry a request body — the body param is attached only to these so a method-dispatching handler doesn't surface a body on its GET branch.

CREATE_CONTEXT = "createContext"
HANDLE_METHOD = "handle"
HTTP_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "TRACE", "CONNECT"]
JAVA_EXTENSION = "java"
PACKAGE_MARKER = "com.sun.net.httpserver"
RE_BODY = /getRequestBody\s*\(\s*\)/
RE_CASE_GROUP = /\bcase\s+("[A-Za-z]+"(?:\s*,\s*"[A-Za-z]+")*)\s*(?:->|:)/

A case label group: one or more verb literals (Java allows case "GET", "HEAD":) terminated by a colon or an arrow (Java 14+ case "GET" -> ...).

RE_EQ_GRM = /"([A-Za-z]+)"\s*==\s*[A-Za-z0-9_.\s]*getRequestMethod\s*\(\s*\)/
RE_EQUALS_GRM = /"([A-Za-z]+)"\s*\.\s*equals(?:IgnoreCase)?\s*\(\s*[A-Za-z0-9_.\s]*getRequestMethod\s*\(\s*\)\s*\)/
RE_GRM_EQ = /getRequestMethod\s*\(\s*\)\s*==\s*"([A-Za-z]+)"/
RE_GRM_EQUALS = /getRequestMethod\s*\(\s*\)\s*\.\s*equals(?:IgnoreCase)?\s*\(\s*"([A-Za-z]+)"/

getRequestMethod() verb comparisons. Hoisted as constants because interpolated regexes recompile on every evaluation.

RE_GRM_VAR_ASSIGN = /\b([A-Za-z_]\w*)\s*=\s*[^;=][^;]*getRequestMethod\s*\(\s*\)/
RE_HEADER = /getRequestHeaders\s*\(\s*\)\s*\.\s*(?:getFirst|get)\s*\(\s*"([^"]+)"/
RE_VERB_LITERAL = /"([A-Za-z]+)"/
SET_HANDLER = "setHandler"
SWITCH_KEYWORD = "switch"

Instance Method Summary

Instance methods inherited from class Analyzer

analyze analyze, base_path : String base_path, base_paths : Array(String) base_paths, callees_needed? : Bool callees_needed?, http_header_name(name : String) : String | Nil http_header_name, line_number_for_index(content : String, char_index : Int32) : Int32 line_number_for_index, logger : NoirLogger logger, parallel_analyze(files : Array(String), &block : String -> Nil) parallel_analyze, read_file_content(path : String) : String read_file_content, result : Array(Endpoint) result, unique_params(params : Array(Param)) : Array(Param) unique_params, url : String url, web_root_path(path : String, markers : Array(String)) : String web_root_path

Constructor methods inherited from class Analyzer

new(options : Hash(String, YAML::Any)) new

Instance methods inherited from module FileHelper

all_files : Array(String) all_files, get_files_by_extension(extension : String) : Array(String) get_files_by_extension, get_files_by_extensions(extensions : Array(String)) : Array(String) get_files_by_extensions, get_files_by_prefix(prefix : String) : Array(String) get_files_by_prefix, get_files_by_prefix_and_extension(prefix : String, extension : String) : Array(String) get_files_by_prefix_and_extension, get_public_dir_files(base_path : String, folder : String) : Array(String) get_public_dir_files, get_public_files(base_path : String, anchors : Array(String) = ["shard.yml", "Gemfile"]) : Array(String) get_public_files

Instance Method Detail

def analyze #

[View source]