zoukankan      html  css  js  c++  java
  • 项目附

    分析FastCGI源码目录下example中echo.c代码:

     1 /*
     2  * echo.c --
     3  *
     4  *    Produce a page containing all FastCGI inputs
     5  *
     6  *
     7  * Copyright (c) 1996 Open Market, Inc.
     8  *
     9  * See the file "LICENSE.TERMS" for information on usage and redistribution
    10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    11  *
    12  */
    13 #ifndef lint
    14 static const char rcsid[] = "$Id: echo.c,v 1.5 1999/07/28 00:29:37 roberts Exp $";
    15 #endif /* not lint */
    16 
    17 #include "fcgi_config.h"
    18 
    19 #include <stdlib.h>
    20 
    21 #ifdef HAVE_UNISTD_H
    22 #include <unistd.h>
    23 #endif
    24 
    25 #ifdef _WIN32
    26 #include <process.h>
    27 #else
    28 extern char **environ;
    29 #endif
    30 
    31 #include "fcgi_stdio.h"
    32 
    33 //输出环境变量
    34 static void PrintEnv(char *label, char **envp)
    35 {
    36     printf("%s:<br>
    <pre>
    ", label);
    37     for ( ; *envp != NULL; envp++) {
    38         printf("%s
    ", *envp);
    39     }
    40     printf("</pre><p>
    ");
    41 }
    42 
    43 int main ()
    44 {
    45     //environ 是指向环境变量 字符串数组(全局指针)
    46     //保存初始状态下的环境变量执政
    47     char **initialEnv = environ;
    48     int count = 0;    //计数
    49 
    50     //阻塞点,有请求来 FCGI_Accept 就返回
    51     while (FCGI_Accept() >= 0) {
    52         //获取 CONTENT_LENGTH 环境变量的值
    53         char *contentLength = getenv("CONTENT_LENGTH");
    54         int len;
    55 
    56     printf("Content-type: text/html
    "
    57         "
    "
    58         "<title>FastCGI echo</title>"
    59         "<h1>FastCGI echo</h1>
    "
    60             "Request number %d,  Process ID: %d<p>
    ", ++count, getpid());
    61 
    62         if (contentLength != NULL) {
    63             //string 转 long
    64             len = strtol(contentLength, NULL, 10);
    65         }
    66         else {
    67             len = 0;
    68         }
    69 
    70         if (len <= 0) {
    71         printf("No data from standard input.<p>
    ");
    72         }
    73         else {
    74             int i, ch;
    75 
    76         printf("Standard input:<br>
    <pre>
    ");
    77             for (i = 0; i < len; i++) {
    78                 if ((ch = getchar()) < 0) {
    79                     printf("Error: Not enough bytes received on standard input<p>
    ");
    80                     break;
    81         }
    82                 putchar(ch);
    83             }
    84             printf("
    </pre><p>
    ");
    85         }
    86         
    87         //打印环境变量
    88         PrintEnv("Request environment", environ);
    89         PrintEnv("Initial environment", initialEnv);
    90     } /* while */
    91 
    92     return 0;
    93 }

    访问web服务器后的页面显示信息:

    http://192.168.1.45/login?user=citrus&passwd=123456

    http请求头的 key 和最后拿到的环境变量的 key 对应关系保存在 nginx fastcgi.conf 文件里面。

    FastCGI echo
    Request number 1, Process ID: 58012
    No data from standard input.
    
    Request environment:
    
    FCGI_ROLE=RESPONDER
    SCRIPT_FILENAME=/usr/local/nginx/html/login
    QUERY_STRING=user=citrus&passwd=123456
    REQUEST_METHOD=GET
    CONTENT_TYPE=
    CONTENT_LENGTH=
    SCRIPT_NAME=/login
    REQUEST_URI=/login?user=citrus&passwd=123456
    DOCUMENT_URI=/login
    DOCUMENT_ROOT=/usr/local/nginx/html
    SERVER_PROTOCOL=HTTP/1.1
    REQUEST_SCHEME=http
    GATEWAY_INTERFACE=CGI/1.1
    SERVER_SOFTWARE=nginx/1.13.10
    REMOTE_ADDR=192.168.1.119
    REMOTE_PORT=60974
    SERVER_ADDR=192.168.1.45
    SERVER_PORT=80
    SERVER_NAME=localhost
    REDIRECT_STATUS=200
    HTTP_ACCEPT=text/html, application/xhtml+xml, image/jxr, */*
    HTTP_ACCEPT_LANGUAGE=zh-CN
    HTTP_ACCEPT_ENCODING=gzip, deflate
    HTTP_HOST=192.168.1.45
    HTTP_CONNECTION=Keep-Alive
    HTTP_USER_AGENT=Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko Core/1.70.3741.400 QQBrowser/10.5.3863.400
    
    
    Initial environment:
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX省略
    博客园文作者:Citrusliu 博文地址:https://www.cnblogs.com/citrus
  • 相关阅读:
    OpenCV 使用FLANN进行特征点匹配
    OpenCV 特征描述
    OpenCV 特征点检测
    OpenCV 亚像素级的角点检测
    OpenCV Shi-Tomasi角点检测子
    OpenCV Harris 角点检测子
    OpenCV 模板匹配
    OpenCV 直方图计算
    OpenCV 直方图均衡化
    OpenCV 仿射变换
  • 原文地址:https://www.cnblogs.com/citrus/p/11977710.html
Copyright © 2011-2022 走看看