Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Web服务器-如何获取URI?_C_Webserver - Fatal编程技术网

C Web服务器-如何获取URI?

C Web服务器-如何获取URI?,c,webserver,C,Webserver,一个简单的cweb服务器:如何获取URI 另外: 也许有一种方法可以简单地查看所有传入的原始数据 例如整个GET请求。。连同URL。。等等 我在网上搜索,但找不到任何信息 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h&g

一个简单的cweb服务器:如何获取URI

另外:
也许有一种方法可以简单地查看所有传入的原始数据

例如整个GET请求。。连同URL。。等等

我在网上搜索,但找不到任何信息

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>

char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"test\r\n";

int main()
{
  int one = 1, client_fd;
  struct sockaddr_in svr_addr, cli_addr;
  socklen_t sin_len = sizeof(cli_addr);

  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    err(1, "can't open socket");

  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

  int port = 82;
  svr_addr.sin_family = AF_INET;
  svr_addr.sin_addr.s_addr = INADDR_ANY;
  svr_addr.sin_port = htons(port);

  if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
    close(sock);
    err(1, "Can't bind");
  }

  listen(sock, 5);
  while (1) {
    client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);
    printf("got connection\n");

    if (client_fd == -1) {
      perror("Can't accept");
      continue;
    }

    write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
    close(client_fd);
  }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
字符响应[]=“HTTP/1.1 200正常\r\n”
“内容类型:text/html;字符集=UTF-8\r\n\r\n”
“测试\r\n”;
int main()
{
int one=1,客户端\u fd;
svr\u addr、cli\u addr中的结构sockaddr\u;
socklen\u t sin\u len=sizeof(cli\u addr);
intsock=socket(AF_INET,sock_STREAM,0);
if(sock<0)
错误(1,“无法打开插座”);
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&one,sizeof(int));
int端口=82;
svr\u addr.sin\u family=AF\u INET;
svr_addr.sin_addr.s_addr=INADDR_ANY;
svr_addr.sin_port=htons(port);
if(bind(sock,(struct sockaddr*)和svr_addr,sizeof(svr_addr))=-1){
关闭(袜子);
错误(1,“无法绑定”);
}
听(短袜,5);
而(1){
client\u fd=accept(sock,(struct sockaddr*)&cli\u addr和sin\u len);
printf(“获得连接\n”);
如果(客户端\u fd==-1){
佩罗尔(“不能接受”);
继续;
}
写入(客户端\u fd,响应,sizeof(响应)-1);/*-1:“\0”*/
关闭(客户_fd);
}
}
根据,GET查询应该如下所示:

GET /index.html HTTP/1.1
Host: www.example.org
所以,当客户端连接时,您必须从客户端读取查询并对其进行解析:

/* data to store client query, warning, should not be big enough to handle all cases */
char query[1024] = "";
char page[128] = "";
char host[128] = "";

/* read query */
if (read(client_fd, query, sizeof query-1) > 0)
{
    char *tok;
    char sep[] * "\r\n";
    char tmp[128];
    /* cut query in lines */
    tok = strtok(query, sep);

    /* process each line */
    while (tok)
    {
        /* See if line contains 'GET' */
        if (1 == sscanf(tok, "GET %s HTTP/1.1", tmp))
        {
            strcpy(page, tmp);
        }
        /* See if line contains 'Host:' */
        else if (1 == sscanf(tok, "Host: %s", tmp))
        {
            strcpy(host, tmp);
        }
        /* get next line */
        tok = strtok(query, sep);
    }
    /* print got data */
    printf("wanted page is: %s%s\n", host, page);
} 
else 
{
    /* handle the error (-1) or no data to read (0) */
}
根据,GET查询应该如下所示:

GET /index.html HTTP/1.1
Host: www.example.org
所以,当客户端连接时,您必须从客户端读取查询并对其进行解析:

/* data to store client query, warning, should not be big enough to handle all cases */
char query[1024] = "";
char page[128] = "";
char host[128] = "";

/* read query */
if (read(client_fd, query, sizeof query-1) > 0)
{
    char *tok;
    char sep[] * "\r\n";
    char tmp[128];
    /* cut query in lines */
    tok = strtok(query, sep);

    /* process each line */
    while (tok)
    {
        /* See if line contains 'GET' */
        if (1 == sscanf(tok, "GET %s HTTP/1.1", tmp))
        {
            strcpy(page, tmp);
        }
        /* See if line contains 'Host:' */
        else if (1 == sscanf(tok, "Host: %s", tmp))
        {
            strcpy(host, tmp);
        }
        /* get next line */
        tok = strtok(query, sep);
    }
    /* print got data */
    printf("wanted page is: %s%s\n", host, page);
} 
else 
{
    /* handle the error (-1) or no data to read (0) */
}