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 の 2 つの関数を何らかの方法でマージすることを意味します ...

編集: 私の間違い、getData はソースからではなく res から抽出する必要があります

4

3 に答える 3

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は、以前に書いた 2 つの awk コマンドを指します。その後、ファイルから出力を読み取ることができますop.txt。30 行のコードからわずか 2 行 (+ を読み取るためのコードop.txt)。これが役立つことを願っています。:) :)

于 2013-09-06T04:31:02.847 に答える