/
on-line-javalex
/
HTTP_and_Web
Обзор
Документация
Войти
/
on-line-javalex
/
HTTP_and_Web
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
HttpRequest.java
74 строки
2 KB
on-line-javalex
upload files
28 фев 2026, 18:55
Верифицирован
28 фев 2026, 18:55
ee36424
Код
Авторство
О чём код?
package netology; import java.io.BufferedReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class HttpRequest { private String method; private String path; private Map<String, String> headers; private byte[] body; public HttpRequest(BufferedReader reader) throws IOException { this.method = ""; this.path = ""; this.headers = new HashMap<>(); this.body = new byte[0]; String requestLine = reader.readLine(); if (requestLine == null || requestLine.isEmpty()) { throw new IOException("Empty request line"); } String[] parts = requestLine.split(" "); if (parts.length != 3) { throw new IOException("Invalid request line: " + requestLine); } this.method = parts[0]; this.path = parts[1]; String line; while ((line = reader.readLine()) != null && !line.isEmpty()) { int colonIndex = line.indexOf(':'); if (colonIndex > 0) { String key = line.substring(0, colonIndex).trim(); String value = line.substring(colonIndex + 1).trim(); headers.put(key, value); } } String contentLengthStr = headers.get("Content-Length"); if (contentLengthStr != null) { try { int contentLength = Integer.parseInt(contentLengthStr); if (contentLength > 0) { char[] charBuffer = new char[contentLength]; int read = reader.read(charBuffer, 0, contentLength); if (read > 0) { this.body = new String(charBuffer, 0, read).getBytes(); } } } catch (NumberFormatException e) { // ignore invalid Content-Length } } } public String getMethod() { return method; } public String getPath() { return path; } public Map<String, String> getHeaders() { return headers; } public byte[] getBody() { return body; } }