#include <sys/stat.h>
#include <iostream>
using namespace std;
#define HOSTLEN 256 //主機名長度
#define BACKLOG 10 //同時等待的連接個數
int sendall(int s, char *buf, int *len) {
int total = 0; // 已經發送字節數
int bytesleft = *len; //還剩餘多少字節
int n;
while(total < *len) {
n = send(s, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
*len = total; // 返回實際發送出去的字節數
return n==-1?-1:0; // 成功發送返回0 失敗-1
}
void wrong_req(int sock) {
char* error_head = "HTTP/1.0 501 Not Implemented\r\n"; //輸出501錯誤
int len = strlen(error_head);
if (sendall(sock, error_head, &len) == -1) { //向客戶發送
printf("Sending failed!");
return;
}
len = strlen(error_type);
if (sendall(sock, error_type, &len) == -1) {
printf("Sending failed!");
return;
}
len = strlen(error_end);
if (sendall(sock, error_end, &len) == -1) {
printf("Sending failed!");
return;
}
len = strlen(prompt_info);
if (sendall(sock, prompt_info, &len) == -1) {
printf("Sending failed!");
return;
}
}
bool not_exit(char* arguments) {
struct stat dir_info;
return (stat(arguments, &dir_info) == -1);
}
void file_not_found(char* arguments, int sock) {
int len = strlen(error_head);
if (sendall(sock, error_head, &len) == -1) { //向客戶端發送
printf("Sending error!");
return;
}
len = strlen(error_type);
if (sendall(sock, error_type, &len) == -1) {
printf("Sending error!");
return;
}
len = strlen(error_end);
if (sendall(sock, error_end, &len) == -1) {
printf("Sending error!");
return;
}
strcat(prompt_info, arguments);
len = strlen(prompt_info);
if (sendall(sock, prompt_info, &len) == -1) { //輸出未找到的文件
printf("Sending error!");
return;
}
}
void send_header(int send_to, char* content_type) {
char* head = "HTTP/1.0 200 OK\r\n"; //正確的頭部信息
int len = strlen(head);
if (sendall(send_to, head, &len) == -1) { //向連接的客戶端發送數據
printf("Sending error");
return;
}
char temp_1[30] = "Content-type: "; //準備好要連接的字串
strcat(temp_1, content_type); //構造content_type
strcat(temp_1, "\r\n");
len = strlen(temp_1);
if (sendall(send_to, temp_1, &len) == -1) {
printf("Sending error!");
return;
}
}
}
char* file_type(char* arg) {
char * temp; //臨時字符串指針
if ((temp=strrchr(arg,'.')) != NULL) { //取得後綴
return temp+1;
}
return ""; //如果請求的文件名中沒有. 則返回空串
}
void send_file(char* arguments, int sock) {
char* content_type = "text/plain"; //初始化type='text/plain'
FILE* read_from; //本地文件指針從該文件中讀取.html .jpg等
int readed = -1; //每次讀得的字節數
if (strcmp(extension, "html") == 0) { //發送內容爲html
content_type = "text/html";
}
content_type = "image/gif";
}
content_type = "image/jpg";
}
if(read_from != NULL) { //指針不爲空
char read_buf[128]; //讀文件時的字節緩存數組
send_header(sock, content_type); //發送協議頭
send(sock, "\r\n", 2, 0); //再加一個"\r\n" 不能缺少 格式要求
fgets(read_buf, 128, read_from); //讀取
int len = strlen(read_buf);
if (sendall(sock, read_buf, &len) == -1) { //發送數據
printf("Sending error!"); //出現發送錯誤顯示到控制檯繼續發送
continue;
}
}
}
}
void handle_req(char* request, int client_sock) {
char arguments[BUFSIZ]; //保存解析到的請求的文件
strcpy(arguments, "./"); //注意該符號在不同操作系統的區別
if (sscanf(request, "%s%s", command, arguments+2) != 2) {
return; //解析出錯在返回
}
printf("handle_cmd: %s\n",command); //向控制檯輸出此時的命令
printf("handle_path: %s\n",arguments); //向控制檯輸出此時的請求路徑
if (strcmp(command, "GET") != 0) { //請求命令格式是否正確
wrong_req(client_sock);
return;
}
file_not_found(arguments, client_sock);
return;
}
return;
}
int make_server_socket() {
struct sockaddr_in server_addr; //服務器地址結構體
if (tempSockId == -1) { //如果返回值爲-1 則出錯
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //本地地址
memset(&(server_addr.sin_zero), '\0', 8);
sizeof(server_addr)) == -1) { //綁定服務如果出錯則返回-1
printf("bind error!\n");
return -1;
}
printf("listen error!\n");
return -1;
}
}
void main(int argc, char * argv[]) {
WSADATA wsaData;
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
int acc_socket; //接收到的用戶連接的socket
int sock_size = sizeof(struct sockaddr_in);
printf("Server exception!\n");
exit(2);
}
while(true) {
acc_socket = accept(server_socket, (struct sockaddr *)&user_socket, &sock_size); //接收連接
//cout << inet_ntoa(user_socket.sin_addr) << endl; //測試用:-)//
int numbytes;
char buf[100];
if ((numbytes=recv(acc_socket, buf, 99, 0)) == -1) {
perror("recv");
exit(1);
}
//printf("buf ... %s", buf); //測試用
handle_req(buf, acc_socket);
}
}