目錄
Web Server
原型
vim HTTPServer.pl
#!/usr/bin/perl
use Socket;
use Crap;
use FileHandle;
# (1) use port 8080 by default, unless overridden on command line
$port = (@ARGV ? $ARGV[0] : 8080);
# (2) create local TCP socket and set it to listen for connections
$proto = getprotobyname('tcp');
socket(S, PF_INET, SOCK_STREAM, $proto) || die;
setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die;
bind(S, sockaddr_in($port, INADDR_ANY)) || die;
listen(S, SOMAXCONN) || die;
# (3) print a startup message
printf("<<<Type-O-Serve Accepting on Port %d>>>\n\n", $port);
while(1)
{
# (4) wait for connection C
$cport_caddr = accept(C, S);
($cport, $caddr) = sockaddr_in($cport_caddr);
C->autoflush(1);
# (5) print who the connection is from
$cname = gethostbyaddr($caddr, AF_INET);
printf("Request From '%s'>>>\n", $cname);
# (6) read request msg until blank line, and print on screen
while ($line = <C>)
{
print $line;
if ($line =~ /^\r/) {
last;
}
}
# (7) prompt for response message, and input response lines,
# sending response lines to clinet, until solitary "."
printf("<<<Type Response Followed by '.'>>>\n");
while ($line = <STDIN>)
{
$line =~ s/\r//;
$line =~ s/\n//;
if ($line =~ /^\./) {
last;
}
print C $line . "\r\n";
}
close(C);
}
cpan crap
perl HTTPServer.pl 8000
<<<Type-O-Serve Accepting on Port 8000>>>
Request From 'localhost'>>>
GET / HTTP/1.1
Host: localhost:8000
User-Agent: curl/7.54.0
Accept: */*
<<<Type Response Followed by '.'>>>
hello world
.
curl localhost:8000
# hello world
作用
接受客戶端連接
接收請求報(bào)文
對資源的映射和訪問
構(gòu)建響應(yīng)
發(fā)送響應(yīng)
記錄日志
方案
Web Application
FastCGI
CGI(Common Gateway Interface)即通用網(wǎng)關(guān)接口 它是Web Server與Web Application之間數(shù)據(jù)交換的一種協(xié)議
HTTP Request的Header設(shè)置成進(jìn)程的環(huán)境變量, HTTP Request的正文設(shè)置成進(jìn)程的標(biāo)準(zhǔn)輸入
進(jìn)程的標(biāo)準(zhǔn)輸出就是HTTP Response, 包括Header和正文
FastCGI(Fast Common Gateway Interface)即快速通用網(wǎng)關(guān)接口 它是CGI的增強(qiáng)版本 通過進(jìn)程管理等優(yōu)化方法提升了服務(wù)性能
PHP-FPM
PHP-FPM = PHP FastCGI Process Manager
web-server-01.png
Tomcat
Tomcat是一個(gè)免費(fèi)的開放源代碼的Servlet容器 它是基于Apache httpd的擴(kuò)展
web-server-02.png
spring init -a demo -b 2.0.6.RELEASE --build gradle -dweb HTTPSDemo
cd HTTPSDemo
vim src/main/java/com/example/demo/DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "hello world";
}
}
- 測試
./gradlew bootrun
curl localhost:8080/hello
# hello world
./gradlew dependencies | grep tomcat
<-------------> 0% EXECUTING [2s]
> :dependencies > Resolve dependencies of :testCompileClasspath
+--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
+--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
+--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34
| \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| | +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| | +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
| | \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34
| | \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| +--- org.springframework.boot:spring-boot-starter-tomcat:2.0.6.RELEASE
| | +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.34
| | +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.34
<-------------> 0% WAITING
> IDLE
keytool -genkey -alias tomcat -dname "CN=Andy,OU=kfit,O=kfit,L=HaiDian,ST=BeiJing,C=CN" -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 365
mv keystore.p12 src/main/resources
vim src/main/resources/application.properties
server.port=443
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=123456
server.ssl.key-alias=tomcat
- 測試
./gradlew bootrun
curl -k https://localhost/hello
# hello world