#include
#include
#include
#include
#include
#include
#include
#include "HttpServer.hpp"
#include "Usage.hpp"
#include "Util.hpp"
// 一般http都要有自己的web根目录
#define ROOT "./wwwroot" // ./wwwroot/index.html
// 如果客户端只请求了一个/,我们返回默认首页
#define HOMEPAGE "index.html"
void HandlerHttpRequest(int sockfd)
{
// 1. 读取请求 for test
char buffer[10240];
ssize_t s = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
if (s > 0)
{
buffer[s] = 0;
// std::cout << buffer << "--------------------\n" << std::endl;
}
std::vector vline;
Util::cutString(buffer, "\n", &vline);
std::vector vblock;
Util::cutString(vline[0], " ", &vblock);// 拿到请求行
std::string file = vblock[1];// cilent想要的文件名
std::string target = ROOT;
if(file == "/") file = "/index.html";
target += file;
std::cout << target << std::endl;
// 打开这个文件,再从这个文件中拿数据
std::string content;
std::ifstream in(target);
if(in.is_open())
{
std::string line;
while(std::getline(in, line))
{
content += line;
}
in.close();
}
std::string HttpResponse;
if(content.empty()) {
HttpResponse = "HTTP/1.1 404 NotFound\r\n";
}
else {
HttpResponse = "HTTP/1.1 200 OK\r\n";
}
HttpResponse += "\r\n";
HttpResponse += content;
// 2. 试着构建一个http的响应
send(sockfd, HttpResponse.c_str(), HttpResponse.size(), 0);
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
Usage(argv[0]);
exit(0);
}
std::unique_ptr httpserver(new HttpServer(atoi(argv[1]), HandlerHttpRequest));
httpserver->Start();
return 0;
}
#pragma once
#include
#include
#include
#include "Sock.hpp"
class HttpServer
{
public:
using func_t = std::function;// 包装器->函数
private:
int listensock_;// 监听
uint16_t port_;
Sock sock;// 套接字
func_t func_;
public:
HttpServer(const uint16_t &port, func_t func): port_(port),func_(func)
{
listensock_ = sock.Socket();
sock.Bind(listensock_, port_);// 绑定
sock.Listen(listensock_);// 监听
}
void Start()
{
signal(SIGCHLD, SIG_IGN);// 解决父进程需要进程等待的问题
for( ; ; )
{
std::string clientIp;
uint16_t clientPort = 0;
int sockfd = sock.Accept(listensock_, &clientIp, &clientPort);
if(sockfd < 0) continue;
if(fork() == 0)
{
close(listensock_);
func_(sockfd);
close(sockfd);
exit(0);
}
close(sockfd);
}
}
~HttpServer()
{
if(listensock_ >= 0) close(listensock_);
}
};
#pragma once
#include
#include
#include
#include
#include
// 日志是有日志级别的
#define DEBUG 0
#define NORMAL 1
#define WARNING 2
#define ERROR 3
#define FATAL 4
const char *gLevelMap[] = {
"DEBUG",
"NORMAL",
"WARNING",
"ERROR",
"FATAL"
};
#define LOGFILE "./calculator.log"
// 完整的日志功能,至少: 日志等级 时间 支持用户自定义(日志内容, 文件行,文件名)
void logMessage(int level, const char *format, ...)
{
#ifndef DEBUG_SHOW
if(level== DEBUG) return;
#endif
char stdBuffer[1024]; //标准部分
time_t timestamp = time(nullptr);
snprintf(stdBuffer, sizeof stdBuffer, "[%s] [%ld] ", gLevelMap[level], timestamp);
char logBuffer[1024]; //自定义部分
va_list args;
va_start(args, format);
vsnprintf(logBuffer, sizeof logBuffer, format, args);
va_end(args);
FILE *fp = fopen(LOGFILE, "a");
fprintf(fp, "%s%s\n", stdBuffer, logBuffer);
fclose(fp);
}
HttpServer:HttpServer.cc
g++ -o $@ $^ -std=c++11 -lpthread
.PHONY:clean
clean:
rm -f HttpServer
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Log.hpp"
class Sock
{
private:
const static int gbacklog = 20;
public:
Sock() {}
// 创建套接字
int Socket()
{
int listensock = socket(AF_INET, SOCK_STREAM, 0);
if (listensock < 0)
{
logMessage(FATAL, "create socket error, %d:%s", errno, strerror(errno));
exit(2);
}
logMessage(NORMAL, "create socket success, listensock: %d", listensock);
return listensock;
}
// 绑定
void Bind(int sock, uint16_t port, std::string ip = "0.0.0.0")
{
struct sockaddr_in local;
memset(&local, 0, sizeof local);
local.sin_family = AF_INET;
local.sin_port = htons(port);
inet_pton(AF_INET, ip.c_str(), &local.sin_addr);
if (bind(sock, (struct sockaddr *)&local, sizeof(local)) < 0)
{
logMessage(FATAL, "bind error, %d:%s", errno, strerror(errno));
exit(3);
}
}
// 监听
void Listen(int sock)
{
if (listen(sock, gbacklog) < 0)
{
logMessage(FATAL, "listen error, %d:%s", errno, strerror(errno));
exit(4);
}
logMessage(NORMAL, "init server success");
}
// 一般经验
// const std::string &: 输入型参数
// std::string *: 输出型参数
// std::string &: 输入输出型参数
int Accept(int listensock, std::string *ip, uint16_t *port)
{
struct sockaddr_in src;
socklen_t len = sizeof(src);
int servicesock = accept(listensock, (struct sockaddr *)&src, &len);
if (servicesock < 0)
{
logMessage(ERROR, "accept error, %d:%s", errno, strerror(errno));// 转移
return -1;
}
if(port) *port = ntohs(src.sin_port);
if(ip) *ip = inet_ntoa(src.sin_addr);
return servicesock;
}
// 连接
bool Connect(int sock, const std::string &server_ip, const uint16_t &server_port)
{
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(server_port);
server.sin_addr.s_addr = inet_addr(server_ip.c_str());
if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == 0) return true;
else return false;
}
~Sock() {}
};
#pragma once
#include
#include
void Usage(std::string proc)
{
std::cout << "\nUsage: " << proc << " port\n" << std::endl;
}
#pragma once
#include
#include
class Util
{
public:
// aaaa\r\nbbbbb\r\nccc\r\n\r\n
// 按sep分割符,分割字符
static void cutString(std::string s, const std::string &sep, std::vector *out)
{
std::size_t start = 0;
while (start < s.size())
{
auto pos = s.find(sep, start);
if (pos == std::string::npos) break;
std::string sub = s.substr(start, pos - start);
// std::cout << "----" << sub << std::endl;
out->push_back(sub);
start += sub.size();
start += sep.size();
}
if(start < s.size()) {
out->push_back(s.substr(start));
}
}
};
当服务端申请CA证书的时候,CA机构会对该服务器进行审核,并专门为该网站形成数字签名,过程如下