【问题标题】:How to get the sub-string lying in between two sub-strings in C?如何获得位于C中两个子字符串之间的子字符串?
【发布时间】:2010-01-17 09:35:51
【问题描述】:

我有一个将 http 有效负载写入文件的数据包捕获代码。现在我想从这些转储中提取 URL 信息。 对于每个数据包,有效负载都是这样开始的。

GET /intl/en_com/images/logo_plain.png HTTP/1.1..主机: www.google.co.in..用户代理: Mozilla/5.0

我想提取:

  1. “GET”和“HTTP/1.1”之间的字符串
  2. “Host:”和“User-Agent”之间的字符串

如何在 C 中做到这一点?是否有任何内置的字符串函数?还是正则表达式?

【问题讨论】:

  • 打几个电话strstr() 可能就足够了。棘手的部分是处理例如 URL 包含“HTTP/1.1”的情况。即使您使用正则表达式库(C 确实存在,但不在标准库中),这个问题也会存在。
  • 好的,我会尝试所有这些并回复您,谢谢!

标签: c url string substring packet-capture


【解决方案1】:

C 没有内置的正则表达式,但有可用的库:http://www.arglist.com/regex/http://www.pcre.org/ 是我最常看到的两个。

对于这么简单的任务,您无需使用正则表达式即可轻松完成任务。如果这些行都小于某个最大长度MAXLEN,则只需一次处理一行:

char buf[MAXLEN];
char url[MAXLEN];
char host[MAXLEN];
int state = 0;      /* 0: Haven't seen GET yet; 1: haven't seen Host yet */
FILE *f = fopen("my_input_file", "rb");

if (!f) {
    report_error_somehow();
}

while (fgets(buf, sizeof buf, f)) {
    /* Strip trailing \r and \n */
    int len = strlen(buf);
    if (len >= 2 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
        buf[len - 2] = 0;
    } else {
        if (feof(f)) {
            /* Last line was not \r\n-terminated: probably OK to ignore */
        } else {
            /* Either the line was too long, or ends with \n but not \r\n. */
            report_error_somehow();
        }
    }

    if (state == 0 && !memcmp(buf, "GET ", 4)) {
        strcpy(url, buf + 4);    /* We know url[] is big enough */
        ++state;
    } else if (state == 1 && !memcmp(buf, "Host: ", 6)) {
        strcpy(host, buf + 6);   /* We know host[] is big enough */
        break;
    }
}

fclose(f);

此解决方案不需要像 KennyTM 的回答那样将整个文件缓冲在内存中(尽管如果您知道文件很小,这很好)。请注意,我们使用fgets() 而不是不安全的gets(),这很容易在长行中溢出缓冲区。

【讨论】:

  • 在 POSIX 系统上,您只需 #include 并使用 regcomp()、regexec() 和 regfree()。
【解决方案2】:

使用strchr(或strstr)查找\r 的位置。由于字符串GETHTTP/1.1Host: 是固定长度的,因此可以轻松提取其间路径的索引和位置。


如果你想使用正则表达式,在 POSIX 兼容的系统上有regcomp(3),但这也很难使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 2013-09-12
    • 2019-12-02
    • 2011-08-07
    • 2015-04-10
    相关资源
    最近更新 更多