C - 尝试发送()数据,但有bug

时间:2014-11-22 12:16:43

标签: c send

我正在尝试使用GET请求逐行发送数据(删除换行符)。问题是,当我检查我的apache访问日志时,请求使用行,其中三个单词一起用空格分隔,即使我对其进行url编码也不会发送(也是2x编码)。

文本文件示例:

asd

asd asd

asd asd asd <- not sent

代码:

asprintf(&httpget, 
        "GET /%s HTTP/1.1\r\n"
        "Host: 127.0.0.1\r\n"
        "User-Agent: \r\n"
        "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,;q=0.5\r\n"
        "Accept-Language: en-us,en;q=0.5\r\n"
        "Accept-Encoding: gzip,deflate\r\n"
        "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
        "Cache-Control: max-age=\r\n"
        "\r\n", line_buffer);

send(socket_handle, httpget, strlen(httpget), 0);

请求应该有效,因为我尝试使用其他脚本复制请求。

所以我发现了一些东西......

当您发送a a时,它会执行请求,但之后不会发送另一行。但我仍然没有想到网络服务器或无效标题的问题,例如在BurpSuite中我可以发送这些网址未编码的数据而没有任何问题,日志看起来像

127.0.0.1 - - [22/Nov/2014:13:37:04 +0100] "GET /test test test HTTP/1.1" 200 54

完整代码:

  socket_handle = socket(AF_INET, SOCK_STREAM, 0) ;
  socket_detials.sin_family = AF_INET ;
  socket_detials.sin_addr.s_addr = inet_addr(host);
  socket_detials.sin_port = htons(80);
  input_buffer = malloc(20000);
  line_number = 0;
  while (fgets(line_buffer, sizeof(line_buffer), infile)) {
    ++line_number;
    strtok(line_buffer, "\n");
    printf("\n\n%s\n\n", line_buffer);
    asprintf(&httpget, 
      "GET /%s HTTP/1.1\r\n"
      "Host: 127.0.0.1\r\n"
      "User-Agent: \r\n"
      "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,;q=0.5\r\n"
      "Accept-Language: en-us,en;q=0.5\r\n"
      "Accept-Encoding: gzip,deflate\r\n"
      "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
      "Cache-Control: max-age=\r\n"
      "\r\n", line_buffer);
    bzero(&(socket_detials.sin_zero), 8);
    connect(socket_handle,(struct sockaddr*)&socket_detials, sizeof(struct sockaddr));
    if(send(socket_handle, httpget, strlen(httpget), 0) < 0) {
  printf("\n\n\n ERROR send() \n\n\n");
    }
    int recieved = recv(socket_handle, input_buffer, 20000, 0);
if(recieved != 0) {
      printf ("\rRecieved %d bytes | Requests made: %d        ", recieved, req++);
      printf("\n%s\n", httpget);
  printf("%s%s\n%s\n", gray, input_buffer, normal);
} else {
  printf ("\rRecieved %d bytes | Requests made: %d        ", recieved, req++);
  printf("\n%sRequest not sent.%s \n\nResponse:\n\n", red, normal);
  printf("\n%s\n", httpget);
  printf("%s%s\n%s\n", gray, input_buffer, normal);
    }
  }

为什么可能无效的请求是套接字关闭?

1 个答案:

答案 0 :(得分:1)

您应该在HTTP RFC中查找URL编码。空格需要编码为&#39;%20&#39;但是还有很多其他字符也需要编码。与将文本文件POST到服务器相比,这看起来效率极低。