2024-06-21
This commit is contained in:
162
Writerside/topics/CGI.md
Normal file
162
Writerside/topics/CGI.md
Normal file
@@ -0,0 +1,162 @@
|
||||
# Common Gateway Interface
|
||||
|
||||
## CGI 환경 변수
|
||||
|
||||
- CONTENT_TYPE
|
||||
|
||||
- CONTENT_LENGTH
|
||||
|
||||
- DOCUMENT_ROOT
|
||||
|
||||
- HTTP_ACCEPT
|
||||
|
||||
- HTTP_COOKIE
|
||||
|
||||
- HTTP_REFERER
|
||||
|
||||
- HTTP_USER_AGENT
|
||||
|
||||
- PATH_INFO
|
||||
|
||||
- QUERY_STRING
|
||||
|
||||
- REMOTE_ADDR
|
||||
|
||||
- REMOTE_HOST
|
||||
|
||||
- REQUEST_METHOD
|
||||
|
||||
- SCRIPT_FILENAME
|
||||
|
||||
- SCRIPT_NAME
|
||||
|
||||
- SERVER_NAME
|
||||
|
||||
- SERVER_PORT
|
||||
|
||||
- SERVER_SOFTWARE
|
||||
|
||||
|
||||
```perl
|
||||
my $buffer;
|
||||
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
|
||||
if ($ENV{'REQUEST_METHOD'} eq "GET") {
|
||||
$buffer = $ENV{'QUERY_STRING'};
|
||||
|
||||
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
|
||||
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
|
||||
|
||||
}
|
||||
|
||||
print "$ENV{SERVER_PROTOCOL} 200 OK\r\n";
|
||||
print "Content-type:text/plain\r\n";
|
||||
print "\r\n";
|
||||
print "Hello\n";
|
||||
```
|
||||
|
||||
## CGI
|
||||
|
||||
```bash
|
||||
sudo cpanm CGI
|
||||
```
|
||||
|
||||
```perl
|
||||
#!/usr/bin/perl
|
||||
use warnings;
|
||||
use strict;
|
||||
|
||||
use CGI;
|
||||
|
||||
my $query = CGI->new;
|
||||
|
||||
$bday = $query->param("birthday");
|
||||
print $query->header();
|
||||
print $query->p("Your birthday is $bday.");
|
||||
```
|
||||
|
||||
- keywords
|
||||
|
||||
- http
|
||||
|
||||
- param
|
||||
|
||||
- append
|
||||
|
||||
- import_names
|
||||
|
||||
- delete
|
||||
|
||||
- delete_all
|
||||
|
||||
- save
|
||||
|
||||
- url
|
||||
|
||||
- header
|
||||
|
||||
- redirect
|
||||
|
||||
- cookie
|
||||
|
||||
- dump
|
||||
|
||||
|
||||
```perl
|
||||
use CGI qw/ :cgi -utf8 /;
|
||||
|
||||
$CGI::DISABLE_UPLOADS = 1;
|
||||
$CGI::POST_MAX = 102_400; # 100 KB
|
||||
|
||||
print $cgi->header('text/html','204 No response');
|
||||
|
||||
my @keywords = $query->keywords;
|
||||
my @names = $query->param;
|
||||
my $value = $query->param('name');
|
||||
|
||||
print $query->redirect('https://example.com');
|
||||
print $query->redirect(
|
||||
-uri => 'https://example.com',
|
||||
-status => '301 Moved Permanently'
|
||||
);
|
||||
```
|
||||
|
||||
POST로 전송된 데이터를 직접 처리해야 하는 경우에는 다음과 같이 데이터를 가져올 수 있습니다.
|
||||
|
||||
```perl
|
||||
my $data = $query->param('POSTDATA');
|
||||
my $data = $query->param('PUTDATA');
|
||||
```
|
||||
|
||||
업로드된 파일은 다음과 같이 처리합니다.
|
||||
|
||||
```perl
|
||||
my $file_handle = $query->upload('uploaded_file');
|
||||
```
|
||||
|
||||
## NGINX와 함께 CGI 사용하기
|
||||
|
||||
[FCGI Wrap | NGINX](https://www.nginx.com/resources/wiki/start/topics/examples/fcgiwrap/)
|
||||
|
||||
```bash
|
||||
sudo apt install fcgiwrap spawn-fcgi
|
||||
cp /usr/share/doc/fcgiwrap/examples/nginx.conf /etc/nginx/fcgiwrap.conf
|
||||
```
|
||||
|
||||
설정 파일에서 필요한 부분은 수정합니다.
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
index index.html;
|
||||
root /var/www;
|
||||
ignore_invalid_headers on;
|
||||
include /etc/nginx/fcgiwrap.conf;
|
||||
location /cgi-bin {
|
||||
gzip off;
|
||||
fastcgi_param SERVER_NAME $http_host;
|
||||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
||||
include /etc/nginx/fastcgi_params;
|
||||
fastcgi_pass unix:/var/run/fcgiwrap.socket;
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user