免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2134 | 回复: 0
打印 上一主题 下一主题

求助高手!高手帮忙分析下httpd.c中的如下代码段,不胜感激! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-01 21:04 |只看该作者 |倒序浏览
小弟在读 httpd源码时其中有一段分析分析客户端请求的代码看不太懂,只大概知道需要从客户端请求字符串中解析出请求的命令,请求的文件,请求的文件类型。代码的细节还望高手帮忙注释下,不胜感激!
int ParseReq(FILE *f, char *r)      
{
  char *bp;
  struct stat stbuf;
  char * arg;
  char * c;
  int e;
  int raw;

#ifdef DEBUG
  printf("req is '%s'\n", r);
#endif
  
  while(*(++r) != ' ');  /*skip non-white space*/    /*r 待解析的字符串指针*/
  while(isspace(*r))  /*一个空格 or not*/
          r++;
  
  while (*r == '/')
          r++;

  bp = r;           
  /**r = '.';*/
  
  while(*r && (*(r) != ' ') && (*(r) != '?'))   
          r++;
         
#ifdef DEBUG
  printf("bp='%s', r='%s'\n", bp, r);
#endif
         
  if (*r == '?')
  {
          char * e;
          *r = 0;
          arg = r+1;
          if (e = strchr(arg,' '))
        {
                  *e = '\0';
          }
  } else
    {
          arg = 0;
          *r = 0;
    }
  
  c = bp;

  if (c[0] == '\0') strcat(c,".");

  if (c && !stat(c, &stbuf))
  {
    if (S_ISDIR(stbuf.st_mode))
    {
            char * end = c + strlen(c);
            strcat(c, "/index.html");
            if (!stat(c, &stbuf))
        {
                    DoHTML(f, c);
            }
        else
        {
                  *end = '\0';
                DoDir(f,c);
        }
    }
    else if (!strcmp(r - 4, ".gif"))
      DoGif(f,c);
        else if (!strcmp(r - 4, ".jpg") || !strcmp(r - 5, ".jpeg"))
          DoJpeg(f,c);
            else if (!strcmp(r - 4, ".htm") || !strcmp(r - 5, ".html"))
              DoHTML(f,c);
                else
                  DoText(f,c);
  } else
    {
          PrintHeader(f,'h');
          alarm(TIMEOUT);
          fprintf(f, "<html><head><title>404 File Not Found</title></head>\n");
        fprintf(f, "<body>The requested URL was not found on this server</body></html>\n");
          alarm(0);
    }
  return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP