1. HTTP 버퍼 구조

김해준's avatar
Jan 21, 2026
1. HTTP 버퍼 구조
💡
request - 들어오는 모든 데이터
response - 내보내는 데이터
header - body 데이터를 표현하는 데이터
body - 전달하려는 데이터

1. HTTP는 “객체”가 아니다

브라우저와 서버는 네트워크 위에서 바이트(문자)만 주고받는다.
HTTP는 그 바이트를 어떤 규칙으로 줄 세워 쓰자는 약속이다.

2. HTTP요청(Request) 구조

요청은 크게 4 부분이다
  1. Request Line (요청 시작 줄)
  1. Headers (메타 데이터)
  1. 빈 줄(header/body 구분)
  1. Body(실제 데이터)
 

3. GET 요청 예시 (Body 없음)

브라우저 주소창에서:
 
/1 → primary 키
ip주소:포트/자원명/ 1
localhost:8000/product/ 1
select * from product where id = 1;
/product/1 ⇒ 패스밸리어블
 
ip주소:포트/자원명?쿼리스트링
localhost:8000/product?name=banana&image=20
select * from product where name = 'banana' and image = '20';
서버로 실제 들어오는 텍스트는 이런 형태다(예시)
notion image
쿼리스트링(Query String) = 구체적인 데이터 요청
  1. Request Line = GET /hello?name=kim&age=20 HTTP/1.1
  1. Headers (메타 데이터) = Host ~ Connection
  1. GET/Accept/Connection ⇒ 기대 요청
  1. Accept = 응답 받기 원하는 데이터의 형태 / Content-Type = 서버로 전달하는 데이터의 형태
  1. Connection = 연결 유지 (중요하지 않음)
예시)
select * from emp where job = 'salesman'; get -> localhost:8080/emp?job=salesman get -> localhost:8080/emp/5 select * from emp where empno = 5
프로토콜은 강제성을 띄지는 않는다.
프로토콜보다 위에 있는것은 서버의 인터페이스.
서버의 인터페이스에 따라 프로토콜이 동작하지 않을 수 있다.

GET 요약(포인트)

  • GET은 보통 조회(Select)
  • 데이터는 URL의 Query String으로 전달
  • Body는 보통 비어있음
 
 

4 POST 요청 예시 (Body 있음)

POST 요청 → DB에 insert 요청
폼 전송(로그인) 같은 경우:
notion image
notion image
notion image
notion image
<Request Line> POST /join HTTP/1.1 <Headers> Host: localhost:8080 Content-Type: application/x-www-form-urlencoded Content-Length: 27 </Headers> <header/body 구분> <Body> username=kim&password=1234
  1. Request Line (요청 시작 줄) = POST /join HTTP/1.1
  1. Headers (메타 데이터) = Host ~ Content-Length
  1. 빈 줄(header/body 구분)
  1. Body(실제 데이터) = username=kim&password=1234
  1. Content-Length = Body의 길이 (\이 없기에 길이를 기준으로 Body 데이터 처리)

json

POST /join HTTP/1.1 Host: localhost:8080 Content-Type: application/json Content-Length: 27 username=kim&password=1234 { "username" : "kim", "password" : 1234 }

POST 요약(포인트)

  • POST는 보통 생성/등록/전송
  • 데이터는 Body로 전달
  • Body를 해석하려면 Content-Type이 중요
 

5. Request 객체가 만들어지는 원리

String buffer = """ POST /join HTTP/1.1 Host: localhost:8080 Content-Type: application/json Content-Length: 27 username=kim&password=1234 """;
String buffer = """ GET /hello.do?name=kim&age=20 HTTP/1.1 Host: localhost:8080 User-Agent: Mozilla/5.0 Accept:text/html Connection: keep-alive """;
notion image
req = 요청 버퍼에 접근하기 위한 객체(Request)
resp = 응답 버퍼에 접근하기 위한 객체(Response)
 
notion image

6. “request/response 객체”는 왜 생겼나?

매 요청마다 우리가 이런 걸 직접 해야 한다면? • request line 파싱 • header 파싱 • query string 파싱 • body 길이(content-length) 읽어서 body 파싱너무 귀찮고 실수도 많다.그래서 톰캣 같은 서블릿 컨테이너가 HTTP 텍스트를 파싱해서 객체로 만들어 준다.

7. HttpServletRequest / HttpServletResponse 개념

HttpServletRequest = “요청 텍스트를 읽기 쉽게 만든 객체” • getMethod() → GET/POST • getRequestURI() → /hello • getQueryString() → name=kim&age=20 • getParameter("name") → kim • getHeader("content-type") → application/json 등HttpServletResponse = “응답 텍스트를 쓰기 쉽게 만든 객체” • setStatus(200) • setContentType("text/html; charset=UTF-8") • getWriter().println(...)
 

8. 예시 코드

main.java

public class App { public static void main(String[] args) throws Exception { // 1. 소켓서버 // 2. 연결이 되면, stream에 BufferedReader // 3. 파싱 String buffer = """ GET /hello.do?name=kim&age=20 HTTP/1.1 Host: localhost:8080 User-Agent: Mozilla/5.0 Accept:text/html Connection: keep-alive """; MyHttpParser request = new MyHttpParser(buffer); System.out.println(request.getMethod()); MyServlet servlet = new MyServlet(); if (request.getMethod().equals("GET")) { servlet.doGet(request); } else { servlet.doPost(request); } } }
MyHttpParser .java
import java.util.HashMap; import java.util.Map; public class MyHttpParser { private String method; private String path; private String protocol; private Map<String, String> headers; public MyHttpParser(String httpRequest) { parse(httpRequest); } private void parse(String httpRequest) { headers = new HashMap<>(); String[] lines = httpRequest.trim().split("\n"); if (lines.length == 0) { return; } // 첫 번째 줄 파싱: 메서드, 경로, 프로토콜 String[] requestLine = lines[0].trim().split("\\s+"); if (requestLine.length >= 3) { this.method = requestLine[0]; this.path = requestLine[1]; this.protocol = requestLine[2]; } // 나머지 줄 파싱: 헤더들 for (int i = 1; i < lines.length; i++) { String line = lines[i].trim(); if (line.isEmpty()) { continue; } 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); } } } public String getMethod() { return method; } public String getPath() { return path; } public String getProtocol() { return protocol; } public Map<String, String> getHeaders() { return headers; } public String getHeader(String key) { return headers.get(key); } }

MyServelt.java

public class MyServlet { public void doGet(MyHttpParser request) { System.out.println("doGet호출됨"); System.out.println(request.getPath()); } public void doPost(MyHttpParser request) { System.out.println("doPost호출됨"); System.out.println(request.getPath()); } }
 
Share article