如何获得子串到"\ r \n\r \n"

Eam*_*orr -1 c string strstr

我有以下字符串:

HTTP/1.1 200 OK
Date: Tue, 06 Dec 2011 11:53:22 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.9
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48
Content-Type: text/html

??(?????I?O????H????????
                          ?4?@??AQ??????t
Run Code Online (Sandbox Code Playgroud)

我想提取"\r\n\r\n",丢弃二进制数据.即:

HTTP/1.1 200 OK
Date: Tue, 06 Dec 2011 11:53:22 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.9
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48
Content-Type: text/html
Run Code Online (Sandbox Code Playgroud)

如何在C中执行此操作?

Cyc*_*ode 6

这可以通过简单地找到前两个连续的换行符来轻松完成,因为这会终止标头,然后将所有内容复制到一个单独的缓冲区:

// find the end of the header, which is terminated
// by two linebreaks
char* end = strstr(header, "\r\n\r\n");
char* buffer = 0;
if(end) {
    // allocate memory to hold the entire header
    buffer = malloc((end - header) + 1);
    if(buffer) {
        // copy header to buffer
        memcpy(buffer, header, end - header);
        // null terminate the buffer
        buffer[end - header] = 0;
        // do something with the buffer

        // don't forget to release the allocated memory
        free(buffer);
    }
}
Run Code Online (Sandbox Code Playgroud)