Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ftinx/webserv into featur…
Browse files Browse the repository at this point in the history
…e/pipelineReqeust
  • Loading branch information
hochan222 committed Apr 19, 2021
2 parents 48e34a4 + 89da33f commit 60463f9
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 28 deletions.
21 changes: 21 additions & 0 deletions config/.sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,25 @@ http {
root PWD/YoupiBanane
}
}
server {
server_name second_server
listen 8081
location / {
limit_except GET
index index.html
}
location /autoindex {
limit_except GET
autoindex on
}
location /redirect {
limit_except GET
return 302 /
}
}
server {
server_name redirect_server
listen 8082
return 301 https://42seoul.kr/
}
}
12 changes: 6 additions & 6 deletions config/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ http {
# root /Users/ftinx/www # Please enter an absolute root path
server {
server_name default_server
listen 1234 # port
listen 1234 # default port 80
default_error_page default_error.html # Change the default error page
# content_length 2048
location / {
limit_except GET # Allowed methods GET HEAD PUT POST OPTIONS DELETE TRACE
# cgi .bin .cgi .bla
# cgi_path /Users/ftinx/cgi-bin/cgi_tester
index index.html index.htm index.php
# limit_body_size 100
# limit_body_size 100 # http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
# root /Users/ftinx/www
auth_basic ftinx
auth_basic_user_file ../.auth/.htpasswd_default # default id/pwd : ftinx/1234
autoindex on
}
location /redirect { # second location block for 301, 302 redirection
location /redirect { # second location block for test'302 Found' redirection
limit_except GET
return 301 /
return 302 /
}
}
server { # second server block for 301 302 redirection to another server
server { # second server block for test '301 Moved Permanently' redirection
server_name redirect_server
listen 9999
return 302 https://42seoul.kr/
return 301 https://42seoul.kr/
}
}
194 changes: 194 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
### ftinx config file 구성, 문법

1. **구성**

- http block

| 구성 | 성격 | 기본값 | 필수 | 유효성 | 예시 |
| ------- | ------- | ------- | ---- | ----------- | ------------------------------- |
| open | bracket | | O | | `http {` |
| name | element | `ftinx` | O | | `software_name ftinx` |
| version | element | `0.1` | O | | `software_version 0.1` |
| include | element | | O | `*.types` | `include mime.types` |
| root | element | | O | `dir exist` | `root /Users/ftinx/Webserv/www` |
| server | block | | O | | `server {` |
| close | bracket | | O | | `}` |
| | | | | | |
- server block

| 구성 | 성격 | 기본값 | 필수 | 유효성 | 예시 |
| ------------------ | ------- | --------- | ---- | ------------------------------- | --------------------------------------- |
| open | bracket | | O | | `server {` |
| server name | element | `default` | O | | `server_name first_server` |
| listen(port) | element | `8080` | O | `1024 ~ 49151` | `listen 8080` |
| defalut error page | element | | | `file exist` | `default_error_page default_error.html` |
| content length | element | | | | `content_length 2048` |
| return | element | | | `method(301 302 307), location` | `return 301 http://localhost:8081/` |
| location | block | | O | | `location /test {` |
| close | bracket | | O | | `}` |
| | | | | | |

- location block

| 구성 | 성격 | 기본값 | 필수 | 유효성 | 예시 |
| -------------------- | ---------------- | --------- | ---- | ------------------------------- | ------------------------------------------- |
| open, path | bracket, element | | O | | `location /test {` |
| limit expect | element(multi) | | O | | `limit_except GET POST` |
| root | element | | | `dir exist` | `root /Users/ftinx/Webserv/Sample2` |
| index | element(multi) | | | `file exist` | `index index.html index.htm index.php` |
| cgi | element(multi) | | | | `cgi .bin .cgi .php` |
| cgi path | element | | | `dir exist` | `root /Users/ftinx/Webserv/Sample2/cgi-bin` |
| limit body size | element | `INT_MAX` | | `0 ~ INT_MAX` | `limit_body_size 100` |
| autoindex | element | `off` | | `on / off` | `autoindex on` |
| auth basic | element(multi) | | | | `auth_basic jwon yechoi holee` |
| auth basic user file | element | | | `file exist` | `auth_basic_user_file .htpasswd` |
| return | element | | | `method(301 302 307), location` | `return 302 /` |
| close | bracket | | O | | `}` |
| | | | | | |

2. **문법**

1. 공통

1. http block은 1개만 유효하다

2. element는 `[KEY][SPACE(s)][VALUE]`의 형식을 따른다

3. `KEY`가 중복되는 경우

1. `VALUE`가 1개만 올 수 있는 element 인 경우 값을 덮어쓴다

```
server {
server_name first
server_name second
```
```
string server_name = second
```
2. `VALUE`가 1개 이상 올 수 있는 multi 성격의 element 인 경우 값을 추가한다
```
server {
cgi .bin
cgi .cgi .php
```
```
vector<string> cgi;
cgi[0] = .bin
cgi[1] = .cgi
cgi[2] = .php
```
4. 정의되지 않은 `KEY`는 무시한다
5. 중괄호(`{`, `}`)
1. 왼쪽 중괄호와 오른쪽 중괄호 개수가 같아야 한다
2. 한개의 라인에 여러개의 중괄호가 올 수 없다
3. `VALUE`에는 중괄호가 포함될 수 없다
6. 주석
1. 주석은 `#comment`의 형식을 따른다
2. 라인의 첫 글자가 `#` 으로 시작한다면 해당 라인은 주석으로 간주하며 파싱하지 않는다
```
#http block 시작
http {
software_name ftinx
software_version 0.1
include mime.types
root /Users/ftinx/Webserv/
#server block 시작
server {
```
3. 라인의 마지막에도 `[SPACE(s)]` 이후 주석을 사용할 수 있다
```
server {
server_name first #서버네임
listen 8080 #포트
```
2. http block
1. server block은 최소 1개 이상이어야 한다
3. server block
1. location block은 최소 1개 이상이어야 한다
2. location block과 return은 같이 존재할 수 없다
4. location block
3. **유효성 검사**
- [x] config file path 인자 값이 비어있는 경우 `default.conf` 파싱
- [x] `KEY` 중복 체크
- [x] 중괄호 체크
- [x] 왼쪽 중괄호와 오른쪽 중괄호 개수 체크
- [x] 한개의 라인에 여러개의 중괄호 여부 체크
- [x] 주석으로만 구성되었거나 비어있는 라인 체크
- [x] http block 존재 여부 체크
- [x] server block 존재 여부 체크
- [x] server block과 return요소 중복 체크
- [x] location block 존재 여부 체크
5. **논의 필요**
1. content-length 관련 [#35 (comment)](https://github.com/ftinx/webserv/issues/35#issuecomment-793454981)
3. 기본값, 필수요소, 유효성체크 필요한 부분 있으면 코멘트 달아주세요
5. **샘플**
1. config/default.conf
```yaml
http {
software_name ftinx
software_version 0.1
include mime.types # 'mime.types' file must be included
# ㄴ https://developer.mozilla.org/ko/docs/Web/HTTP/Basics_of_HTTP/MIME_types
# ㄴ https://www.nginx.com/resources/wiki/start/topics/examples/full/#mime-types
default_type application/octet-stream
# ㄴ https://developer.mozilla.org/en-US/docs/Learn/Server-side/Configuring_server_MIME_types
# root /Users/ftinx/www # Please enter an absolute root path
server {
server_name default_server
listen 1234 # port
default_error_page default_error.html # Change the default error page
# content_length 2048
location / {
limit_except GET # Allowed methods GET HEAD PUT POST OPTIONS DELETE TRACE
# cgi .bin .cgi .bla
# cgi_path /Users/ftinx/cgi-bin/cgi_tester
index index.html index.htm index.php
# limit_body_size 100 # http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
# root /Users/ftinx/www
auth_basic ftinx
auth_basic_user_file ../.auth/.htpasswd_default # default id/pwd : ftinx/1234
autoindex on
}
location /redirect { # second location block for test'302 Found' redirection
limit_except GET
return 302 /
}
}
server { # second server block for test '301 Moved Permanently' redirection
server_name redirect_server
listen 9999
return 301 https://42seoul.kr/
}
}
```





4 changes: 2 additions & 2 deletions includes/HttpConfigLocation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HttpConfigLocation
bool m_autoindex;
std::vector<std::string> m_auth_basic;
std::string m_auth_basic_user_file;
int m_limit_body_size;
long m_limit_body_size;
std::vector<std::string> m_redirect;

public:
Expand All @@ -36,7 +36,7 @@ class HttpConfigLocation
bool get_m_autoindex() const;
std::vector<std::string> get_m_auth_basic() const;
std::string get_m_auth_basic_user_file() const;
int get_m_limit_body_size() const;
long get_m_limit_body_size() const;
std::vector<std::string> get_m_redirect() const;

/* setter */
Expand Down
2 changes: 1 addition & 1 deletion includes/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Server
// bool checkHttpConfigFilePath(std::string path, std::string method="");
// bool checkHttpConfigFilePathHead(std::string path);
std::string getMimeType(std::string extension);
std::string makeAutoindexPage(std::string root, std::string path);
std::string makeAutoindexPage(std::string root, std::string abs_path, std::string loc_path);

Response methodGET(int clientfd, std::string method="GET");

Expand Down
2 changes: 2 additions & 0 deletions includes/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace ft
bool isdigit(int c);
bool isprint(int c);
int atoi(const char *str);
long atol(const char *str);
size_t strlen(const char *str);
void putchar_fd(const char c, int fd);
void putstr_fd(const char *s, int fd);
Expand All @@ -73,6 +74,7 @@ namespace ft
/* LIBFT C++ */
std::string strTolower(const std::string &str);
int stoi(const std::string &str);
long stol(const std::string &str);
char* strdup(const std::string &str);
std::string ltrim(const std::string &str, const std::string &set);
std::string rtrim(const std::string &str, const std::string &set);
Expand Down
20 changes: 17 additions & 3 deletions srcs/HttpConfigLocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ HttpConfigLocation::get_m_auth_basic_user_file() const
return (this->m_auth_basic_user_file);
}

int
long
HttpConfigLocation::get_m_limit_body_size() const
{
return (this->m_limit_body_size);
Expand Down Expand Up @@ -248,7 +248,17 @@ HttpConfigLocation::parseLocationBlock(std::vector<std::string> lines, std::stri
this->m_auth_basic_user_file = line.back();
}
else if (line.front().compare("limit_body_size") == 0)
this->m_limit_body_size = ft::stoi(line.back());
{
char *tmp;
std::strtol(line.back().c_str(), &tmp, 10);
if (*tmp)
throw std::exception();
this->m_limit_body_size = ft::stol(line.back());
if (this->m_limit_body_size == 0)
this->m_limit_body_size = INT_MAX;
else if (this->m_limit_body_size < 0 || this->m_limit_body_size > INT_MAX)
throw std::exception();
}
else if (line.front().compare("return") == 0)
{
if (this->m_redirect.empty() == false)
Expand All @@ -263,9 +273,13 @@ HttpConfigLocation::parseLocationBlock(std::vector<std::string> lines, std::stri
this->m_redirect.push_back(line[i]);
}
char *tmp;
strtol(this->m_redirect.front().c_str(), &tmp, 10);
std::strtol(this->m_redirect.front().c_str(), &tmp, 10);
if (*tmp || this->m_redirect.size() != 2)
throw std::exception();
if (this->m_redirect[0].compare("301") != 0 &&
this->m_redirect[0].compare("302") != 0 &&
this->m_redirect[0].compare("307") != 0) // 301 302 307 확인 후 아니면 에러
throw std::exception();
}
else if (line.front().compare("}") == 0)
{
Expand Down
20 changes: 16 additions & 4 deletions srcs/HttpConfigServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/*============================================================================*/

HttpConfigServer::HttpConfigServer():
m_server_name(""),
m_listen(0),
m_server_name("default"),
m_listen(8080),
m_default_error_page(""),
m_content_length(0),
m_location_block(),
Expand Down Expand Up @@ -119,7 +119,15 @@ HttpConfigServer::parseServerBlock(std::vector<std::string> lines, std::string r
if (line.front().compare("server_name") == 0)
this->m_server_name = line.back();
else if (line.front().compare("listen") == 0)
this->m_listen = stoi(line.back());
{
char *tmp;
std::strtol(line.back().c_str(), &tmp, 10);
if (*tmp)
throw std::exception();
this->m_listen = ft::stoi(line.back());
if (this->m_listen < 1024 || this->m_listen >= 49151)
throw std::exception();
}
else if (line.front().compare("default_error_page") == 0)
{
if (ft::isValidFilePath(root + std::string("/") + line.back()) == false)
Expand All @@ -142,9 +150,13 @@ HttpConfigServer::parseServerBlock(std::vector<std::string> lines, std::string r
this->m_redirect.push_back(line[i]);
}
char *tmp;
strtol(this->m_redirect[0].c_str(), &tmp, 10);
std::strtol(this->m_redirect[0].c_str(), &tmp, 10);
if (*tmp || this->m_redirect.size() != 2)
throw std::exception();
if (this->m_redirect[0].compare("301") != 0 &&
this->m_redirect[0].compare("302") != 0 &&
this->m_redirect[0].compare("307") != 0) // 301 302 307 확인 후 아니면 에러
throw std::exception();
return_exist = true;
}
else if (line.front().compare("location") == 0)
Expand Down
Loading

0 comments on commit 60463f9

Please sign in to comment.