【问题标题】:regex to validate HTTP request string in C?正则表达式验证 C 中的 HTTP 请求字符串?
【发布时间】:2015-05-15 13:55:51
【问题描述】:

我对正则表达式了解不多,但我知道它有什么用处。我想在 C 中验证这样的 HTTP 请求。

GET /foo.html HTTP/1.1

以下内容是强制性的

  1. 获取
  2. "/"
  3. 文件扩展名(文件路径中的句点)
  4. HTTP/1.1
  5. 只有两个空格,一个在 GET 之后,另一个在文件路径之后。

我编写的代码没有正则表达式,它真的很乱,它使用 strncmp、2 个 for 循环和一些 bools 来验证请求。我仍然不知道如何验证 HTTP/1.1。这是代码(注意 - “行”是存储 HTTP 请求的字符数组)

// validate request-line
        if (strncmp(line, "GET", 3) != 0)
            error(405);
        if (line[3] != ' ')
            error(400);
        if (line[4] != '/')
            error(501);

        // Initializing some variable for future use
        bool period = false;
        int y = strlen(line);
        bool secondspace = false;

        // quotes not allowed
        for (int z = 0; z < y; z++)
        {
            if (line[z] == '"')
                error(400);
        }

        // check for period in file path
        for (int x = 4; x < y ; x++)
        {
            if (secondspace != true)
            {
                if (line[x] == '.')
                {
                    period = true;
                }
            }
            if (line[x] != ' ')
            {
                secondspace = true;
            }

        }
        if (period != true)
            error(501);
        if (secondspace != true)
            error(400);

【问题讨论】:

  • 我是否正确假设您不仅要验证,还要提取信息?
  • 那种。我想验证这 5 件事,如果其中任何一个无效,则必须发送相应的错误。
  • 还有,女士们,先生们,这就是您在没有真正解决问题的情况下做出的回应......
  • @Nem 句点不保证文件扩展名。
  • @Karoly 是的,你可以假设我也想提取信息。

标签: c regex http


【解决方案1】:

您可以使用 PCRE C-API。 (最新的是 10.10)

下载页面:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

我向您展示我自己的使用此 API 的“preg_match”函数:

int preg_match( char *pattern, char *subject, char *match ) {
  int rc, error;
  PCRE2_SIZE erroffset, *ovector;
  pcre2_code *re;
  pcre2_match_data *match_data;

  size_t len = strlen( (char *) subject );

  re = pcre2_compile( (PCRE2_SPTR) pattern, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, NULL );

  if ( !re ) {
    syslog( LOG_ERR, "preg_match(): pcre_compile failed (offset: %zu), %d", erroffset, error );
    return (-1);
  }

  match_data = pcre2_match_data_create_from_pattern( re, NULL );

  rc = pcre2_match( re, (PCRE2_SPTR) subject, len, 0, 0, match_data, NULL );

  if ( rc < 0 ) {
    switch ( rc ) {
      case PCRE2_ERROR_NOMATCH:
        syslog( LOG_ERR, "preg_match(): Pattern '%s' no match!", pattern );
        break;
      default:
        syslog( LOG_ERR, "preg_match(): Matching error %d!", rc );
        break;
    }

    pcre2_match_data_free( match_data );
    pcre2_code_free( re );
    return (-2);
  }

  ovector = pcre2_get_ovector_pointer( match_data );

  PCRE2_SPTR substring_start = (PCRE2_SPTR) subject + ovector[2];
  size_t substring_length = ovector[3] - ovector[2];
  sprintf( match, "%.*s", (int) substring_length, (char *) substring_start );

  pcre2_match_data_free( match_data );
  pcre2_code_free( re );

  return (rc);
}

你可以这样在 C 程序中使用:

int main() {
  char *match,
       *pattern,
       *subject;

  pattern = "GET /(\\S*) HTTP/1.1";
  subject = "GET /foo.html HTTP/1.1";

  match = (char *) malloc(100);
  memset( match, 0, sizeof( match ) );

  if ( preg_match( pattern, subject, match ) > 0 ) {
    printf("File: %s\n", match);
  }
  else {
    printf("No match!\n");
  }

  free( match );

  return (0);
}

输出:

文件:foo.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多