|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 European Union |
| 3 | + * |
| 4 | + * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent |
| 5 | + * versions of the EUPL (the "Licence"); You may not use this work except in compliance with the Licence. |
| 6 | + * |
| 7 | + * You may obtain a copy of the Licence at: |
| 8 | + * |
| 9 | + * https://interoperable-europe.ec.europa.eu/collection/eupl/eupl-text-eupl-12 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an |
| 12 | + * "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licence for |
| 13 | + * the specific language governing permissions and limitations under the Licence. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.gitb.engine.messaging.handlers.layer.application.http; |
| 17 | + |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.OutputStream; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +/** |
| 27 | + * Minimal HTTP/1.1 request/status line, header and body read/write helpers, replacing the wire-level |
| 28 | + * functionality previously provided by the Apache HttpCore {@code DefaultBHttpClientConnection} / |
| 29 | + * {@code DefaultBHttpServerConnection} classes used by {@link HttpSender} and {@link HttpReceiver}. |
| 30 | + * <p> |
| 31 | + * This is a deliberately narrow implementation, matching only what those two classes relied on: a request/status |
| 32 | + * line, a flat list of headers, and a body sized by the {@code Content-Length} header (always sent by |
| 33 | + * {@link HttpSender}, defaulting to {@code 0} otherwise). Chunked transfer encoding is not supported, as it was |
| 34 | + * never produced or consumed by this handler. |
| 35 | + */ |
| 36 | +final class Http11Wire { |
| 37 | + |
| 38 | + static final String CRLF = "\r\n"; |
| 39 | + |
| 40 | + private Http11Wire() { |
| 41 | + } |
| 42 | + |
| 43 | + static void writeRequestLine(OutputStream out, String method, String path) throws IOException { |
| 44 | + writeLine(out, method + " " + path + " HTTP/1.1"); |
| 45 | + } |
| 46 | + |
| 47 | + static void writeStatusLine(OutputStream out, int statusCode, String reasonPhrase) throws IOException { |
| 48 | + writeLine(out, "HTTP/1.1 " + statusCode + " " + reasonPhrase); |
| 49 | + } |
| 50 | + |
| 51 | + static void writeHeaders(OutputStream out, Map<String, String> headers) throws IOException { |
| 52 | + for (Map.Entry<String, String> header : headers.entrySet()) { |
| 53 | + writeLine(out, header.getKey() + ": " + header.getValue()); |
| 54 | + } |
| 55 | + writeLine(out, ""); |
| 56 | + } |
| 57 | + |
| 58 | + static void writeBody(OutputStream out, byte[] body) throws IOException { |
| 59 | + if (body != null && body.length > 0) { |
| 60 | + out.write(body); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static void writeLine(OutputStream out, String line) throws IOException { |
| 65 | + out.write(line.getBytes(StandardCharsets.ISO_8859_1)); |
| 66 | + out.write('\r'); |
| 67 | + out.write('\n'); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Reads a single CRLF (or bare LF)-terminated line. Returns {@code null} if the stream is at EOF before any |
| 72 | + * bytes are read (i.e. the peer closed the connection without sending anything further). |
| 73 | + */ |
| 74 | + static String readLine(InputStream in) throws IOException { |
| 75 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 76 | + int read; |
| 77 | + boolean any = false; |
| 78 | + while ((read = in.read()) != -1) { |
| 79 | + any = true; |
| 80 | + if (read == '\n') { |
| 81 | + break; |
| 82 | + } |
| 83 | + if (read != '\r') { |
| 84 | + buffer.write(read); |
| 85 | + } |
| 86 | + } |
| 87 | + if (!any) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + return buffer.toString(StandardCharsets.ISO_8859_1); |
| 91 | + } |
| 92 | + |
| 93 | + static LinkedHashMap<String, String> readHeaders(InputStream in) throws IOException { |
| 94 | + LinkedHashMap<String, String> headers = new LinkedHashMap<>(); |
| 95 | + String line; |
| 96 | + while ((line = readLine(in)) != null && !line.isEmpty()) { |
| 97 | + int separator = line.indexOf(':'); |
| 98 | + if (separator > 0) { |
| 99 | + headers.put(line.substring(0, separator).trim(), line.substring(separator + 1).trim()); |
| 100 | + } |
| 101 | + } |
| 102 | + return headers; |
| 103 | + } |
| 104 | + |
| 105 | + static byte[] readBody(InputStream in, int contentLength) throws IOException { |
| 106 | + if (contentLength <= 0) { |
| 107 | + return new byte[0]; |
| 108 | + } |
| 109 | + return in.readNBytes(contentLength); |
| 110 | + } |
| 111 | + |
| 112 | + /** Case-insensitive header lookup, matching the case-insensitive header semantics of HTTP. */ |
| 113 | + static String getHeaderIgnoreCase(Map<String, String> headers, String name) { |
| 114 | + for (Map.Entry<String, String> entry : headers.entrySet()) { |
| 115 | + if (entry.getKey().equalsIgnoreCase(name)) { |
| 116 | + return entry.getValue(); |
| 117 | + } |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + static String reasonPhrase(int statusCode) { |
| 123 | + return switch (statusCode) { |
| 124 | + case 200 -> "OK"; |
| 125 | + case 201 -> "Created"; |
| 126 | + case 202 -> "Accepted"; |
| 127 | + case 204 -> "No Content"; |
| 128 | + case 301 -> "Moved Permanently"; |
| 129 | + case 302 -> "Found"; |
| 130 | + case 304 -> "Not Modified"; |
| 131 | + case 400 -> "Bad Request"; |
| 132 | + case 401 -> "Unauthorized"; |
| 133 | + case 403 -> "Forbidden"; |
| 134 | + case 404 -> "Not Found"; |
| 135 | + case 405 -> "Method Not Allowed"; |
| 136 | + case 500 -> "Internal Server Error"; |
| 137 | + case 502 -> "Bad Gateway"; |
| 138 | + case 503 -> "Service Unavailable"; |
| 139 | + default -> ""; |
| 140 | + }; |
| 141 | + } |
| 142 | +} |
0 commit comments