Table of Contents
原创性申明:
%i %s
Home Backend Development PHP Tutorial C语言实现php服务器

C语言实现php服务器

Jun 23, 2016 pm 01:46 PM

原理介绍

原创性申明:

HTTP协议的作用原理

连接:Web浏览器与Web服务器建立连接,打开一个称为socket(套接字)的虚拟文件,此文件的建立标志着连接建立成功。
请求:Web浏览器通过socket向Web服务器提交请求。HTTP的请求一般是GET或POST命令(POST用于FORM参数的传递)。GET命令的格式为:
GET 路径/文件名 HTTP/1.0
文件名指出所访问的文件,HTTP/1.0指出Web浏览器使用的HTTP版本。

应答:Web浏览器提交请求后,通过HTTP协议传送给Web服务器。Web服务器接到后,进行事务处理,处理结果又通过HTTP传回给Web浏览器,从而在Web浏览器上显示出所请求的页面。


然后返回 网页内容到客户端,客户端更具 Conntent-Type 来解析


代码实现

其实Web服务器也就是一个一般的Socket的服务器,只不过它遵守HTTP协议来实现的罢了


//// server.c//// David J. Malan// malan@harvard.edu//// feature test macro requirements#define _GNU_SOURCE#define _XOPEN_SOURCE 700#define _XOPEN_SOURCE_EXTENDED// limits on an HTTP request's size, based on Apache's// http://httpd.apache.org/docs/2.2/mod/core.html#define LimitRequestFields 50#define LimitRequestFieldSize 4094#define LimitRequestLine 8190// number of octets for buffered reads#define OCTETS 512// header files#include <stdio.h>#include <arpa>#include <errno.h>#include <limits.h>#include <math.h>#include <signal.h>#include <stdbool.h>#include <stdlib.h>#include <string.h>#include <strings.h>#include <unistd.h>// typestypedef char octet;// prototypesbool connected(void);bool error(unsigned short code);void handler(int signal);ssize_t load(void);const char* lookup(const char* extension);ssize_t parse(void);void reset(void);void start(short port, const char* path);void stop(void);// server's rootchar* root = NULL;// file descriptor for socketsint cfd = -1, sfd = -1;// buffer for requestoctet* request = NULL;// FILE pointer for filesFILE* file = NULL;// buffer for response-bodyoctet* body = NULL;int main(int argc, char* argv[]){	// a global variable defined in errno.h that's "set by system	// calls and some library functions [to a nonzero value]	// in the event of an error to indicate what went wrong"	errno = 0;	// default to a random port	int port = 0;	// usage	const char* usage = "Usage: server [-p port] /path/to/root";	// parse command-line arguments	int opt;	while ((opt = getopt(argc, argv, "hp:")) != -1)	{		switch (opt)		{		// -h		case 'h':			printf("%s\n", usage);			return 0;			// -p port		case 'p':			port = atoi(optarg);			break;		}	}	// ensure port is a non-negative short and path to server's root is specified	if (port  SHRT_MAX || argv[optind] == NULL || strlen(argv[optind]) == 0)	{		// announce usage		printf("%s\n", usage);		// return 2 just like bash's builtins		return 2;	}	// start server	start(port, argv[optind]);	// listen for SIGINT (aka control-c)	signal(SIGINT, handler);	// accept connections one at a time	while (true)	{		// reset server's state		reset();		// wait until client is connected		if (connected())		{			// parse client's HTTP request			ssize_t octets = parse();			if (octets == -1)			{				continue;			}			// extract request's request-line			// http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html			const char* haystack = request;			char* needle = strstr(haystack, "\r\n");			if (needle == NULL)			{				error(400);				continue;			}			else if (needle - haystack + 2 > LimitRequestLine)			{				error(414);				continue;			}			char line[needle - haystack + 2 + 1];			strncpy(line, haystack, needle - haystack + 2);			line[needle - haystack + 2] = '\0';			// log request-line			printf("\n%s", line);			// TODO: validate request-line			char* method = line;			char* request_target;			char* version;			char* absolute_path;			char* s1;			s1 = strstr (line," ");			if (s1 == NULL || (version = strstr(s1 + 1, " ")) == NULL || strncmp(line + sizeof(line) - 2, "\r\n", 2) == 0)			{				error(400);				continue;			}			request_target = strdup(s1 + 1);			request_target[version - s1 - 1] = '\0';			//printf("request_target is %s, version is %s", request_target, version);			if (strncmp(method,"GET",3) != 0)			{				error(405);				continue;			}			if (strncmp(request_target, "/", 1) != 0)			{				error(501);				continue;			}			if (strchr(request_target, '\"') != NULL)			{				error(400);				continue;			}			if (strncmp (version + 1,"HTTP/1.1",8) != 0)			{				error(505);				continue;			}			if (strchr(request_target, '.') == NULL)			{				error(501);				continue;			}			// TODO: extract query from request-target			char *query = strchr(request_target, '?');			if (query == NULL)				query = "";			else				query = query + 1;			//printf("query is %s\n", query);			// TODO: concatenate root and absolute-path			absolute_path = strdup(request_target);//			printf("%s. %d\n", absolute_path, query-request_target);			if (strcmp(query, "") != 0)				absolute_path[query - request_target - 1] = '\0';			//printf("absolute path is %s\n", absolute_path);			char *path = (char*)malloc(1000);			strcpy(path, root);//			strcat(path, "/");			strcat(path, absolute_path);			//printf("path is %s\n", path);			// TODO: ensure path exists			if (access(path, F_OK) == -1) {				error(404);				continue;			}			// TODO: ensure path is readable			if (access(path, R_OK) == -1) {				error(403);				continue;			}			// TODO: extract path's extension			char* extension;			if ((extension = strrchr(absolute_path, '.')) == NULL) {				error(501);				continue;			}			extension = extension + 1;			//printf("extension is %s\n", extension);			// dynamic content			if (strcasecmp("php", extension) == 0)			{				// open pipe to PHP interpreter				char* format = "QUERY_STRING=\"%s\" REDIRECT_STATUS=200 SCRIPT_FILENAME=\"%s\" php-cgi";				char command[strlen(format) + (strlen(path) - 2) + (strlen(query) - 2) + 1];				sprintf(command, format, query, path);				file = popen(command, "r");				if (file == NULL)				{					error(500);					continue;				}				// load file				ssize_t size = load();				if (size == -1)				{					error(500);					continue;				}				// subtract php-cgi's headers from body's size to get content's length				haystack = body;				needle = memmem(haystack, size, "\r\n\r\n", 4);				if (needle == NULL)				{					error(500);					continue;				}				size_t length = size - (needle - haystack + 4);				// respond to client				if (dprintf(cfd, "HTTP/1.1 200 OK\r\n")  599)	{		return false;	}	// determine Status-Line's phrase	// http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1	const char* phrase = NULL;	switch (code)	{	case 400: phrase = "Bad Request"; break;	case 403: phrase = "Forbidden"; break;	case 404: phrase = "Not Found"; break;	case 405: phrase = "Method Not Allowed"; break;	case 413: phrase = "Request Entity Too Large"; break;	case 414: phrase = "Request-URI Too Long"; break;	case 418: phrase = "I'm a teapot"; break;	case 500: phrase = "Internal Server Error"; break;	case 501: phrase = "Not Implemented"; break;	case 505: phrase = "HTTP Version Not Supported"; break;	}	if (phrase == NULL)	{		return false;	}	// template	char* template = "<title>%i %s</title>
<h1 id="i-s">%i %s</h1>";	char content[strlen(template) + 2 * ((int) log10(code) + 1 - 2) + 2 * (strlen(phrase) - 2) + 1];	int length = sprintf(content, template, code, phrase, code, phrase);	// respond with Status-Line	if (dprintf(cfd, "HTTP/1.1 %i %s\r\n", code, phrase)  0)		{			body = realloc(body, size + octets);			if (body == NULL)			{				return -1;			}			memcpy(body + size, buffer, octets);			size += octets;		}		// check for EOF		if (feof(file) != 0)		{			break;		}	}	return size;}/** * Handles signals. */void handler(int signal){	// control-c	if (signal == SIGINT)	{		// ensure this isn't considered an error		// (as might otherwise happen after a recent 404)		errno = 0;		// announce stop		printf("\033[33m");		printf("Stopping server\n");		printf("\033[39m");		// stop server		stop();	}}/** * Returns MIME type for supported extensions, else NULL. */const char* lookup(const char* extension){	// TODO	if (strcasecmp(extension, "css"))			return "text/css";	if (strcasecmp(extension, "html"))			return "text/html";	if (strcasecmp(extension, "gif"))			return "image/gif";	if (strcasecmp(extension, "ico"))			return "image/x-icon";	if (strcasecmp(extension, "jpg"))			return "image/jpeg";	if (strcasecmp(extension, "js"))			return "text/javascript";	if (strcasecmp(extension, "png"))			return "image/png";	return NULL;}/** * Parses an HTTP request. */ssize_t parse(void){	// ensure client's socket is open	if (cfd == -1)	{		return -1;	}	// ensure request isn't already parsed	if (request != NULL)	{		return -1;	}	// buffer for octets	octet buffer[OCTETS];	// parse request	ssize_t length = 0;	while (true)	{		// read from socket		ssize_t octets = read(cfd, buffer, sizeof(octet) * OCTETS);		if (octets == -1)		{			error(500);			return -1;		}		// if octets have been read, remember new length		if (octets > 0)		{			request = realloc(request, length + octets);			if (request == NULL)			{				return -1;			}			memcpy(request + length, buffer, octets);			length += octets;		}		// else if nothing's been read, socket's been closed		else		{			return -1;		}		// search for CRLF CRLF		int offset = (length - octets = LimitRequestLine + LimitRequestFields * LimitRequestFieldSize)		{			error(413);			return -1;		}	}	return length;}/** * Resets server's state, deallocating any resources. */void reset(void){	// free response's body	if (body != NULL)	{		free(body);		body = NULL;	}	// close file	if (file != NULL)	{		fclose(file);		file = NULL;	}	// free request	if (request != NULL)	{		free(request);		request = NULL;	}	// close client's socket	if (cfd != -1)	{		close(cfd);		cfd = -1;	}}/** * Starts server. */void start(short port, const char* path){	// path to server's root	root = realpath(path, NULL);	if (root == NULL)	{		stop();	}	// ensure root exists	if (access(root, F_OK) == -1)	{		stop();	}	// ensure root is executable	if (access(root, X_OK) == -1)	{		stop();	}	// announce root	printf("\033[33m");	printf("Using %s for server's root, size is %ld", root, sizeof(root));	printf("\033[39m\n");	// create a socket	sfd = socket(AF_INET, SOCK_STREAM, 0);	if (sfd == -1)	{		stop();	}	// allow reuse of address (to avoid "Address already in use")	int optval = 1;	setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));	// assign name to socket	struct sockaddr_in serv_addr;	memset(&serv_addr, 0, sizeof(serv_addr));	serv_addr.sin_family = AF_INET;	serv_addr.sin_port = htons(port);	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);	if (bind(sfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1)	{		stop();	}	// listen for connections	if (listen(sfd, SOMAXCONN) == -1)	{		stop();	}	// announce port in use	struct sockaddr_in addr;	socklen_t addrlen = sizeof(addr);	if (getsockname(sfd, (struct sockaddr*) &addr, &addrlen) == -1)	{		stop();	}	printf("\033[33m");	printf("Listening on port %i", ntohs(addr.sin_port));	printf("\033[39m\n");}/** * Stop server, deallocating any resources. */void stop(void){	// preserve errno across this function's library calls	int errsv = errno;	// reset server's state	reset();	// free root, which was allocated by realpath	if (root != NULL)	{		free(root);	}	// close server socket	if (sfd != -1)	{		close(sfd);	}	// terminate process	if (errsv == 0)	{		// success		exit(0);	}	else	{		// announce error		printf("\033[33m");		printf("%s", strerror(errsv));		printf("\033[39m\n");		// failure		exit(1);	}}</unistd.h></strings.h></string.h></stdlib.h></stdbool.h></signal.h></math.h></limits.h></errno.h></arpa></stdio.h>
Copy after login


编译方法
gcc -o server server.c -lm

运行方法

./server -p 8080 public 

其中public目录是存放html和php文件的





Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles