C语言Web开发:CGI、FastCGI、Nginx深度解析

C语言Web开发:CGI、FastCGI、Nginx深度解析

C语言Web开发:CGI、FastCGI、Nginx深度解析

在这里插入图片描述

一、前言:为什么Web开发是C语言开发的重要技能?

学习目标

  • 理解Web开发的本质:编写程序实现Web应用、服务器端逻辑和客户端交互
  • 明确Web开发的重要性:支撑互联网、电子商务、社交网络等领域的发展
  • 掌握本章学习重点:CGI、FastCGI、Nginx的开发方法、避坑指南、实战案例分析
  • 学会使用C语言开发Web应用,实现服务器端逻辑和客户端交互

重点提示

💡 Web开发是C语言开发的重要技能!随着互联网的普及,Web开发的需求越来越大,C语言的高性能和可移植性使其在Web开发中具有重要地位。


二、模块1:CGI(通用网关接口)基础

2.1 学习目标

  • 理解CGI的本质:通用网关接口,用于Web服务器与服务器端程序之间的通信
  • 掌握CGI的核心架构:Web服务器、CGI程序、客户端
  • 掌握CGI的开发方法:使用C语言编写CGI程序
  • 掌握CGI的避坑指南:避免环境变量未设置、避免输出格式错误、避免资源泄漏
  • 避开CGI使用的3大常见坑

2.2 CGI的核心架构

Web服务器:接受客户端请求,将请求转发给CGI程序
CGI程序:处理请求,生成响应
客户端:发送请求,接收响应

2.3 CGI的开发方法

代码示例1:CGI程序——简单的Hello World

#include<stdio.h>#include<stdlib.h>intmain(){// 设置HTTP响应头printf("Content-Type: text/plain\n\n");// 输出响应内容printf("Hello from CGI!");return0;}

代码示例2:CGI程序——获取HTTP请求参数

#include<stdio.h>#include<stdlib.h>#include<string.h>voiddecode_url(char*src,char*dst){int i =0, j =0;while(src[i]){if(src[i]=='%'){int value;sscanf(src + i +1,"%2x",&value); dst[j++]=(char)value; i +=3;}elseif(src[i]=='+'){ dst[j++]=' '; i++;}else{ dst[j++]= src[i++];}} dst[j]='\0';}intmain(){// 获取环境变量char*query_string =getenv("QUERY_STRING");char*content_type =getenv("CONTENT_TYPE");char*request_method =getenv("REQUEST_METHOD");// 输出环境变量信息printf("Content-Type: text/plain\n\n");printf("Query String: %s\n", query_string ? query_string :"");printf("Content Type: %s\n", content_type ? content_type :"");printf("Request Method: %s\n", request_method ? request_method :"");if(strcmp(request_method,"GET")==0&& query_string){char*token =strtok(query_string,"&");while(token){char*equals =strchr(token,'=');if(equals){*equals ='\0';char*key = token;char*value = equals +1;char decoded_key[100], decoded_value[100];decode_url(key, decoded_key);decode_url(value, decoded_value);printf("Parameter: %s = %s\n", decoded_key, decoded_value);} token =strtok(NULL,"&");}}return0;}

三、模块2:FastCGI(快速通用网关接口)基础

3.1 学习目标

  • 理解FastCGI的本质:快速通用网关接口,改进了CGI的性能
  • 掌握FastCGI的核心架构:Web服务器、FastCGI进程、客户端
  • 掌握FastCGI的开发方法:使用C语言编写FastCGI程序
  • 掌握FastCGI的避坑指南:避免进程管理错误、避免通信错误、避免资源泄漏
  • 避开FastCGI使用的3大常见坑

3.2 FastCGI的核心架构

Web服务器:接受客户端请求,将请求转发给FastCGI进程
FastCGI进程:处理请求,生成响应,保持进程驻留以提高性能
客户端:发送请求,接收响应

3.3 FastCGI的开发方法

代码示例3:FastCGI程序——简单的Hello World

#include<fcgi_stdio.h>#include<stdlib.h>intmain(){while(FCGI_Accept()>=0){// 设置HTTP响应头printf("Content-Type: text/plain\n\n");// 输出响应内容printf("Hello from FastCGI!");}return0;}

代码示例4:FastCGI程序——获取HTTP请求参数

#include<fcgi_stdio.h>#include<stdlib.h>#include<string.h>voiddecode_url(char*src,char*dst){int i =0, j =0;while(src[i]){if(src[i]=='%'){int value;sscanf(src + i +1,"%2x",&value); dst[j++]=(char)value; i +=3;}elseif(src[i]=='+'){ dst[j++]=' '; i++;}else{ dst[j++]= src[i++];}} dst[j]='\0';}intmain(){while(FCGI_Accept()>=0){// 获取环境变量char*query_string =getenv("QUERY_STRING");char*content_type =getenv("CONTENT_TYPE");char*request_method =getenv("REQUEST_METHOD");// 输出环境变量信息printf("Content-Type: text/plain\n\n");printf("Query String: %s\n", query_string ? query_string :"");printf("Content Type: %s\n", content_type ? content_type :"");printf("Request Method: %s\n", request_method ? request_method :"");if(strcmp(request_method,"GET")==0&& query_string){char*token =strtok(query_string,"&");while(token){char*equals =strchr(token,'=');if(equals){*equals ='\0';char*key = token;char*value = equals +1;char decoded_key[100], decoded_value[100];decode_url(key, decoded_key);decode_url(value, decoded_value);printf("Parameter: %s = %s\n", decoded_key, decoded_value);} token =strtok(NULL,"&");}}}return0;}

四、模块3:Nginx与C语言开发基础

4.1 学习目标

  • 理解Nginx的本质:高性能Web服务器和反向代理服务器
  • 掌握Nginx的核心架构:事件驱动模型、内存池、多进程模型
  • 掌握Nginx的开发方法:使用C语言编写Nginx模块
  • 掌握Nginx的避坑指南:避免模块编译错误、避免内存泄漏、避免线程安全问题
  • 避开Nginx使用的3大常见坑

4.2 Nginx的核心架构

事件驱动模型:使用epoll等事件通知机制,高效处理并发连接
内存池:统一管理内存分配和释放,避免内存泄漏
多进程模型:Master进程管理Worker进程,Worker进程处理请求

4.3 Nginx的开发方法

代码示例5:Nginx模块——简单的Hello World

#include<ngx_config.h>#include<ngx_core.h>#include<ngx_http.h>staticngx_int_tngx_http_hello_handler(ngx_http_request_t*r);staticngx_command_t ngx_http_hello_commands[]={{ngx_string("hello_world"), NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS, ngx_conf_set_flag_slot, NGX_HTTP_LOC_CONF_OFFSET,0,NULL}, ngx_null_command };staticngx_http_module_t ngx_http_hello_module_ctx ={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};ngx_module_t ngx_http_hello_module ={ NGX_MODULE_V1,&ngx_http_hello_module_ctx, ngx_http_hello_commands, NGX_HTTP_MODULE,NULL,NULL,NULL,NULL,NULL,NULL,NULL, NGX_MODULE_V1_PADDING };staticngx_int_tngx_http_hello_handler(ngx_http_request_t*r){ngx_int_t rc;ngx_buf_t*b;ngx_chain_t out;// 设置响应头 r->headers_out.content_type.len =sizeof("text/plain")-1; r->headers_out.content_type.data =(u_char *)"text/plain"; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n =13;// 发送响应头 rc =ngx_http_send_header(r);if(rc == NGX_ERROR || rc > NGX_OK || r->header_only){return rc;}// 准备响应内容 b =ngx_pcalloc(r->pool,sizeof(ngx_buf_t));if(b ==NULL){return NGX_HTTP_INTERNAL_SERVER_ERROR;} out.buf = b; out.next =NULL; b->pos =(u_char *)"Hello from Nginx!"; b->last = b->pos +13; b->memory =1; b->last_buf =1;// 发送响应内容returnngx_http_output_filter(r,&out);}staticngx_int_tngx_http_hello_init(ngx_conf_t*cf){ ngx_http_handler_pt *h;ngx_http_core_loc_conf_t*clcf; clcf =ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); h =ngx_array_push(&clcf->handlers);if(h ==NULL){return NGX_ERROR;}*h = ngx_http_hello_handler;return NGX_OK;}staticngx_http_module_t ngx_http_hello_module_ctx ={NULL, ngx_http_hello_init,NULL,NULL,NULL,NULL,NULL,NULL};

五、模块4:实战案例分析——使用C语言实现简单的Web应用

5.1 学习目标

  • 掌握使用C语言实现简单的Web应用:通过Nginx和FastCGI实现一个简单的Web应用
  • 学会使用FastCGI程序处理HTTP请求,解析参数,生成响应
  • 避开实战案例使用的3大常见坑

5.2 使用C语言实现简单的Web应用

代码示例6:FastCGI程序——用户登录

#include<fcgi_stdio.h>#include<stdlib.h>#include<string.h>voiddecode_url(char*src,char*dst){int i =0, j =0;while(src[i]){if(src[i]=='%'){int value;sscanf(src + i +1,"%2x",&value); dst[j++]=(char)value; i +=3;}elseif(src[i]=='+'){ dst[j++]=' '; i++;}else{ dst[j++]= src[i++];}} dst[j]='\0';}intmain(){while(FCGI_Accept()>=0){// 获取环境变量char*content_type =getenv("CONTENT_TYPE");char*request_method =getenv("REQUEST_METHOD");if(strcmp(request_method,"POST")==0){// 获取请求体长度char*content_length_str =getenv("CONTENT_LENGTH");int content_length =atoi(content_length_str);// 读取请求体char*post_data =(char*)malloc(content_length +1);if(post_data){fread(post_data,1, content_length,stdin); post_data[content_length]='\0';// 解析请求体参数char*username =NULL;char*password =NULL;char*token =strtok(post_data,"&");while(token){char*equals =strchr(token,'=');if(equals){*equals ='\0';char*key = token;char*value = equals +1;char decoded_key[100], decoded_value[100];decode_url(key, decoded_key);decode_url(value, decoded_value);if(strcmp(decoded_key,"username")==0){ username =strdup(decoded_value);}elseif(strcmp(decoded_key,"password")==0){ password =strdup(decoded_value);}} token =strtok(NULL,"&");}// 验证用户printf("Content-Type: text/plain\n\n");if(username && password &&strcmp(username,"admin")==0&&strcmp(password,"123456")==0){printf("登录成功!");}else{printf("用户名或密码错误!");}free(username);free(password);free(post_data);}}else{// 发送登录页面printf("Content-Type: text/html\n\n");printf("<html>");printf("<head>");printf("<title>登录页面</title>");printf("</head>");printf("<body>");printf("<h1>用户登录</h1>");printf("<form method='post' action='/login'>");printf("用户名:<input type='text' name='username'><br>");printf("密码:<input type='password' name='password'><br>");printf("<input type='submit' value='登录'>");printf("</form>");printf("</body>");printf("</html>");}}return0;}

Nginx配置文件示例:

server { listen 80; server_name localhost; location / { root html; index index.html; } location /login { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.cgi; include fastcgi_params; } } 

六、本章总结与课后练习

6.1 总结

CGI网络编程:通用网关接口,用于Web服务器与服务器端程序之间的通信
FastCGI网络编程:改进了CGI的性能,支持进程驻留
Nginx与C语言开发:高性能Web服务器和反向代理服务器,支持模块开发
实战案例分析:使用C语言实现简单的Web应用,包含登录功能

6.2 课后练习

  1. 编写程序:实现一个简单的CGI程序,输出当前时间
  2. 编写程序:实现一个简单的CGI程序,获取HTTP请求的Cookie
  3. 编写程序:实现一个简单的FastCGI程序,输出当前时间
  4. 编写程序:实现一个简单的FastCGI程序,获取HTTP请求的Cookie
  5. 编写程序:实现一个简单的Nginx模块,输出当前时间
  6. 编写程序:实现一个简单的Nginx模块,获取HTTP请求的Cookie
  7. 编写程序:实现一个简单的Web应用,包含用户注册和登录功能
  8. 编写程序:实现一个简单的Web应用,包含数据存储和查询功能
  9. 编写程序:实现一个简单的Web应用,包含文件上传和下载功能
  10. 编写程序:实现一个简单的Web应用,包含WebSocket通信功能

Read more

MySQL 内置函数指南:日期、字符串、数学函数实战

MySQL 内置函数指南:日期、字符串、数学函数实战

🔥草莓熊Lotso:个人主页 ❄️个人专栏: 《C++知识分享》《Linux 入门到实践:零基础也能懂》 ✨生活是默默的坚持,毅力是永久的享受! 🎬 博主简介: 文章目录 * 前言: * 一. 日期函数:处理时间相关需求 * 1.1 核心日期函数表 * 1.2 实战案例 * 1.2.1 基础时间获取 * 1.2.2 日期加减运算 * 1.2.3 日期差计算与时间提取 * 1.2.4 业务场景:查询近期数据 * 二. 字符串函数:处理文本数据 * 2.1 核心字符串函数表 * 2.2 实战案例 * 2.2.

By Ne0inhk
Go map 底层原理

Go map 底层原理

Go map 底层原理 * 1. 一语戳破哈希表 * 2. 经典版:Go map 到底长什么样 * 2.1 `hmap` 解决什么问题 * 2.2 `bmap` 解决什么问题 * 2.3 `tophash[8]` 到底在干什么 * 2.4 `overflow bucket` 是怎么来的 * 3. 扩容不是“多加几个桶”那么简单 * 3.1 为什么旧桶必须搬 * 3.2 为什么 Go 要做渐进式扩容 * 3.3 增量扩容和等量扩容 * 4. 并发安全:原生 map 为什么不能裸奔 * 5. 现版本的Go

By Ne0inhk
微服务学习笔记(2)——SpringCloud Nacos

微服务学习笔记(2)——SpringCloud Nacos

🔥我的主页:九转苍翎⭐️个人专栏:《Java SE 》《Java集合框架系统精讲》《MySQL高手之路:从基础到高阶 》《计算机网络 》《Java工程师核心能力体系构建》《RabbitMQ理论与实践》天行健,君子以自强不息。 0.前言 * SpringBoot版本:3.2.5 * SpringCloud版本:2023.0.3 * SpringCloud Alibaba版本:2023.0.1.0 * nacos版本:2.2.3(已免费上传至我的资源) * 项目源码:spring-cloud-blog 1.概述 Nacos(Dynamic Naming and Configuration Service)是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置和管理平台。在 Spring Cloud 体系中,

By Ne0inhk
Rust异步Web框架Axum的深入原理与高级用法

Rust异步Web框架Axum的深入原理与高级用法

Rust异步Web框架Axum的深入原理与高级用法 一、Axum框架的架构与核心组件 1.1 Axum框架的设计理念 💡Axum是基于Tokio异步运行时的Rust Web框架,由Tokio团队官方维护,具有以下核心设计理念: 1. 模块化与可扩展性:通过中间件、请求提取器和响应映射器等组件,实现高度模块化的架构,允许开发者根据需求灵活组合功能。 2. 类型安全:利用Rust的类型系统确保请求处理逻辑的正确性,减少运行时错误。 3. 异步优先:完全基于Tokio异步运行时,充分利用现代硬件的并发能力。 4. 低门槛:提供简单易用的API,同时保持足够的灵活性,适合不同经验水平的开发者。 1.2 Axum框架的核心组件 1.2.1 请求提取器 请求提取器负责从HTTP请求中提取所需的数据,如路径参数、查询参数、请求体等。Axum提供了多种内置的请求提取器,并允许开发者自定义提取器。 内置请求提取器示例: useaxum::{extract::Path,response::IntoResponse,routing::get,

By Ne0inhk