0

我将以下 http 回复保存在名为 source.txt 的本地文件中:

HTTP/1.1 301 Moved
Connection: close
Content-length: 111
Location: https://11.12.13.14:81/
Content-type: text/html; charset="utf-8"

<html><head><META HTTP-EQUIV="refresh" CONTENT="0;URL=https://11.12.13.14:81/"></head><body></body></html>

和以下代码:

#include <stdio.h>
#include <stdlib.h>
#define MAXBUFLEN 1024

char* getLocation(char* source)
{
    const char *p1 = strstr(source, "Location:")+10;
    const char *p2 = strstr(p1, "\n");
    size_t len = p2-p1;
    char *res = (char*)malloc(sizeof(char)*(len+1));
    strncpy(res, p1, len);
    res[len] = '\0';
    return res;
}

char* getData(char* source)
{
    const char *p1 = strstr(source, "://")+3;
    const char *p2 = strstr(p1, "\n");
    size_t len = p2-p1;
    char *res = (char*)malloc(sizeof(char)*(len+1));
    strncpy(res, p1, len);
    res[len] = '\0';
    return res;
}

int main()
{
    char source[MAXBUFLEN];
    char host[100];
    int port;
    FILE *fp = fopen("source.txt", "r");
    if (fp != NULL) {
        size_t newLen = fread(source, sizeof(char), MAXBUFLEN, fp);
        if (newLen == 0) {
            fputs("Error reading file", stderr);
        } else {
            source[++newLen] = '\0';

//extraction code
            char* line = getLocation(source);
            printf("getLocation result: %s\n", line);
            if (strstr(line, "://"))
            {
                char* res = getData(line);//here is the error
                printf("getData result: %s\n", res);
                if (strstr(res, ":"))
                {
                    sscanf(res, "%[^:]:%d[^/]", host, &port);
                    printf("host: %s | port: %d\n", host, port);
                }
                else
                    printf("delimiter not found\n");
            }
            else
                printf("no link\n");
//
        }
    }
    fclose(fp);
}

该程序运行良好,但非常难看。

有没有办法改进代码以避免做这么多操作?

我的意思是以某种方式合并这两个函数 getLocation 和 getData ...

编辑:我的错误,getData 必须从 res 而不是从源中提取

4

3 回答 3

0

这样的事情将是显而易见的方式:

char * getstuff(char * source, char * label) {
    const char *p1 = strstr(source, label) + strlen(label);
    const char *p2 = strstr(p1, "\n");
    size_t len = p2-p1;

    char *res = malloc(len+1);
    if ( res == NULL ) {
        fputs("Couldn't allocate memory.", stderr);
        exit(EXIT_FAILURE);
    }

    strncpy(res, p1, len);
    res[len] = '\0';

    return res;
}

char* getLocation(char* source) {
    return getstuff(source, "Location: ");
}

char* getData(char* source) {
    return getstuff(source, "://");
}

或者只是拥有和完全getstuff()省略,如果你只打算调用每个函数一次。getLocation()getData()

于 2013-09-06T04:19:06.830 回答
0

假设您正在研究linux,我在 awk 中有一个答案:

awk '///:/{print $2}' source.txt 

会像你的一样行事getLocation()

我怀疑getData()实际上应该给你html content(但你的代码返回的字符串与getLocation()但没有http://)。所以,这是我awk获取html内容的代码。

awk '/<html>/{print $0}' source.txt

将为您提供 html 回复的实际内容。(当然我假设\n内容中没有字符。但可以轻松扩展)。

要将其集成到您的代码中,只需执行以下操作:

system("command >> op.txt");

wherecommand指的是我之前写的两个 awk 命令。然后,您可以从文件中读取输出op.txt。30 行代码到只有 2 行(+ 一些代码来阅读op.txt)。我希望这有帮助。:) :)

于 2013-09-06T04:31:02.847 回答
0
#include <stdio.h>
#include <stdlib.h>
#define MAXBUFLEN 1024

char* getLocation(char* source)
{
    const char *p1 = strstr(source, "Location:")+10;
    const char *p2 = strstr(p1, "\n");
    size_t len = p2-p1;
    char *res = (char*)malloc(sizeof(char)*(len+1));
    strncpy(res, p1, len);
    res[len] = '\0';
    return res;
}

int main()
{
    char source[MAXBUFLEN];
    char host[100];
    int port;
    FILE *fp = fopen("source.txt", "r");
    if (fp != NULL) {
        size_t newLen = fread(source, sizeof(char), MAXBUFLEN, fp);
        if (newLen == 0) {
            fputs("Error reading file", stderr);
        } else {
            source[++newLen] = '\0';

//extraction code
            char* res = getLocation(source);
            printf("getLocation result: %s\n", res);
            res = strstr(res,"://");
            if (res != NULL)
            {
                res = res+3;
                if (strstr(res, ":"))
                {
                    sscanf(res, "%[^:]:%d[^/]", host, &port);
                    printf("host: %s | port: %d\n", host, port);
                }
                else
                    printf("delimiter not found\n");
            }
            else
                printf("no link\n");
//
        }
    }
    fclose(fp);
}
于 2013-09-06T05:53:54.797 回答