์น ํ๋ก๊ทธ๋จ ๊ตฌํ ์ , ํ๋กํ ์ฝ ์์ฒญ์ด ์ด๋ป๊ฒ ๋ค์ด์ค๋ ์ง ์์๋ณด๊ณ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋์์๋ฆฌ๋ฅผ ์์๋ณด๋๋ก ํ๋ค.
requet line
GET /calculate?operand1=11&operator=*&operand2=55 HTTP/1.1
๋ค์๊ณผ ๊ฐ์ ์์ฒญ์ด ๋ค์ด์ค๋ฉด ์๋์ ๊ฐ์ด ๋๋ ์ ์๋ค.
- GET ์์ฒญ
- Path Variable : /calculate
- ์ฟผ๋ฆฌ ์คํธ๋ง : ?operand1=11&operator=*&operand2=55
- ํ๋กํ ์ฝ ๋ฐ ๋ฒ์ : HTTP/1.1
Path variable๊ณผ ์ฟผ๋ฆฌ ์คํธ๋ง์ ์ฐจ์ด์ ์ ๋ํด์๋ ์๋ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ์ฝ๊ฒ ์ ์ ์๋ค.
path์ ์ฟผ๋ฆฌ ์คํธ๋ง์ ์ฐจ์ด ์์๋ณด๊ธฐ

๋ค์๊ณผ ๊ฐ์ด ์๋ฒ๋ฅผ ๋์์์ผฐ์ ๋, request ์ฒซ๋ฒ์งธ ๋ผ์ธ(request)์ splitํด /calculate?operand1=11&operator=*&operand2=55 ์ด ๋ถ๋ถ์ ์ถ์ถํ๋ค.
RequestLine
๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ RequestLine์ ๋๋ ์ ์์๊น ?
๋จผ์ split(" ")์ ํตํด GET, url, ํ๋กํ ์ฝ ๋ถ๋ถ์ ๋๋์ด ์ค ๋ค.
๊ทธ๋ ๊ฒ ๋๋ ์ฒซ๋ฒ์งธ ํ ํฐ์ method์ ์ ์ฅํ๊ณ , ๋๋ฒ์งธ ํ ํฐ์ ?๋ฅผ ๊ธฐ์ค์ผ๋ก ๋๋๋ค๋ฉด path variable๋ถ๋ถ๊ณผ query string์ผ๋ก ๋๋ ์ ์๋ค.
์ด ๋, ์ฐธ๊ณ ๋ก split ๋ด๋ถ์ ?์ ๊ฐ์ ํน์๋ฌธ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ split("\\?")์ ๊ฐ์ด ์ ์ด์ฃผ์ด์ผ ์ค๋ฅ๊ฐ ๋์ง ์๋๋ค. split("")์์ ๋ฌธ์์ด ๋ถ๋ถ์ด ์ ๊ทํํ์์ ๋ฐ๋ผ์ผํ๊ธฐ ๋๋ฌธ์ด๋ค. ์์ธํ regex(์ ๊ทํํ์)์ ๋ํ ์ฌํญ์ ๋ค์ ๋งํฌ๋ฅผ ํตํด ์ ์ ์๋ค.
regex ๋ ์์๋ณด๊ธฐ
public RequestLine(String requestLine) {
String[] tokens = requestLine.split(" ");
this.method = tokens[0]; // GET
String[] urlPathtokens = tokens[1].split("\\?");
this.urlPath = urlPathtokens[0]; // /calculate
// ์ฟผ๋ฆฌ ์คํธ๋ง์ด ์๋ ๊ฒฝ์ฐ
if (urlPathtokens.length == 2) {
this.queryString = urlPathtokens[1]; // operand1=11&operator=*&operand2=55
}
}
public RequestLine(String method, String urlPath, String queryString) {
this.method = method;
this.urlPath = urlPath;
this.queryString = queryString;
}
๋ค์๊ณผ ๊ฐ์ด ๋๊ฐ์ ์์ฑ์๋ฅผ ๋ง๋ค์ด ResquestLine์ด ์ ์์ ์ผ๋ก ๋์ํ๋ ์ง test ํด๋ณธ๋ค.
RequestLineTest
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RequestLineTest {
@Test
void create() {
RequestLine requestLine = new RequestLine("GET /calculate?operand1=11&operator=*&operand2=55 HTTP/1.1");
assertThat(requestLine).isNotNull();
assertThat(requestLine).isEqualTo(new RequestLine("GET", "/calculate","operand1=11&operator=*&operand2=55"));
}
}
๋ค์์๋ ์ด๋ ๊ฒ ๋ถ๋ฆฌํ Query String ์ค์์๋ ๊ฐ key์ value๋ค์ ์ถ์ถํด๋ณผ ์์ ์ด๋ค.
'๐ web' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Flutter] ๊พน ๋๋ฅด๊ธฐ ํจ๊ณผ ์ฃผ๊ธฐ (2) | 2024.10.01 |
---|---|
[github] github.io๋ก ๊ฐ๋ฐ์ ํฌํธํด๋ฆฌ์ค ์์ฑํ๊ธฐ ๐๐ป (1) | 2024.09.10 |
web | ์น๊ฐ๋ฐ ์ผ์ง - ์์ํ๊ธฐ, ํ ์คํธ ์ฝ๋ (0) | 2023.08.08 |
์น ํ๋ก๊ทธ๋จ ๊ตฌํ ์ , ํ๋กํ ์ฝ ์์ฒญ์ด ์ด๋ป๊ฒ ๋ค์ด์ค๋ ์ง ์์๋ณด๊ณ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋์์๋ฆฌ๋ฅผ ์์๋ณด๋๋ก ํ๋ค.
requet line
GET /calculate?operand1=11&operator=*&operand2=55 HTTP/1.1
๋ค์๊ณผ ๊ฐ์ ์์ฒญ์ด ๋ค์ด์ค๋ฉด ์๋์ ๊ฐ์ด ๋๋ ์ ์๋ค.
- GET ์์ฒญ
- Path Variable : /calculate
- ์ฟผ๋ฆฌ ์คํธ๋ง : ?operand1=11&operator=*&operand2=55
- ํ๋กํ ์ฝ ๋ฐ ๋ฒ์ : HTTP/1.1
Path variable๊ณผ ์ฟผ๋ฆฌ ์คํธ๋ง์ ์ฐจ์ด์ ์ ๋ํด์๋ ์๋ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ์ฝ๊ฒ ์ ์ ์๋ค.
path์ ์ฟผ๋ฆฌ ์คํธ๋ง์ ์ฐจ์ด ์์๋ณด๊ธฐ

๋ค์๊ณผ ๊ฐ์ด ์๋ฒ๋ฅผ ๋์์์ผฐ์ ๋, request ์ฒซ๋ฒ์งธ ๋ผ์ธ(request)์ splitํด /calculate?operand1=11&operator=*&operand2=55 ์ด ๋ถ๋ถ์ ์ถ์ถํ๋ค.
RequestLine
๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ RequestLine์ ๋๋ ์ ์์๊น ?
๋จผ์ split(" ")์ ํตํด GET, url, ํ๋กํ ์ฝ ๋ถ๋ถ์ ๋๋์ด ์ค ๋ค.
๊ทธ๋ ๊ฒ ๋๋ ์ฒซ๋ฒ์งธ ํ ํฐ์ method์ ์ ์ฅํ๊ณ , ๋๋ฒ์งธ ํ ํฐ์ ?๋ฅผ ๊ธฐ์ค์ผ๋ก ๋๋๋ค๋ฉด path variable๋ถ๋ถ๊ณผ query string์ผ๋ก ๋๋ ์ ์๋ค.
์ด ๋, ์ฐธ๊ณ ๋ก split ๋ด๋ถ์ ?์ ๊ฐ์ ํน์๋ฌธ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ split("\\?")์ ๊ฐ์ด ์ ์ด์ฃผ์ด์ผ ์ค๋ฅ๊ฐ ๋์ง ์๋๋ค. split("")์์ ๋ฌธ์์ด ๋ถ๋ถ์ด ์ ๊ทํํ์์ ๋ฐ๋ผ์ผํ๊ธฐ ๋๋ฌธ์ด๋ค. ์์ธํ regex(์ ๊ทํํ์)์ ๋ํ ์ฌํญ์ ๋ค์ ๋งํฌ๋ฅผ ํตํด ์ ์ ์๋ค.
regex ๋ ์์๋ณด๊ธฐ
public RequestLine(String requestLine) {
String[] tokens = requestLine.split(" ");
this.method = tokens[0]; // GET
String[] urlPathtokens = tokens[1].split("\\?");
this.urlPath = urlPathtokens[0]; // /calculate
// ์ฟผ๋ฆฌ ์คํธ๋ง์ด ์๋ ๊ฒฝ์ฐ
if (urlPathtokens.length == 2) {
this.queryString = urlPathtokens[1]; // operand1=11&operator=*&operand2=55
}
}
public RequestLine(String method, String urlPath, String queryString) {
this.method = method;
this.urlPath = urlPath;
this.queryString = queryString;
}
๋ค์๊ณผ ๊ฐ์ด ๋๊ฐ์ ์์ฑ์๋ฅผ ๋ง๋ค์ด ResquestLine์ด ์ ์์ ์ผ๋ก ๋์ํ๋ ์ง test ํด๋ณธ๋ค.
RequestLineTest
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RequestLineTest {
@Test
void create() {
RequestLine requestLine = new RequestLine("GET /calculate?operand1=11&operator=*&operand2=55 HTTP/1.1");
assertThat(requestLine).isNotNull();
assertThat(requestLine).isEqualTo(new RequestLine("GET", "/calculate","operand1=11&operator=*&operand2=55"));
}
}
๋ค์์๋ ์ด๋ ๊ฒ ๋ถ๋ฆฌํ Query String ์ค์์๋ ๊ฐ key์ value๋ค์ ์ถ์ถํด๋ณผ ์์ ์ด๋ค.
'๐ web' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Flutter] ๊พน ๋๋ฅด๊ธฐ ํจ๊ณผ ์ฃผ๊ธฐ (2) | 2024.10.01 |
---|---|
[github] github.io๋ก ๊ฐ๋ฐ์ ํฌํธํด๋ฆฌ์ค ์์ฑํ๊ธฐ ๐๐ป (1) | 2024.09.10 |
web | ์น๊ฐ๋ฐ ์ผ์ง - ์์ํ๊ธฐ, ํ ์คํธ ์ฝ๋ (0) | 2023.08.08 |