regex to validate HTTP request string in C? -


i don't know regex know how it's useful. want validate http requests 1 in c.

get /foo.html http/1.1 

following things compulsory

  1. get
  2. "/"
  3. file extension (period in file path)
  4. http/1.1
  5. only 2 spaces, 1 after , after filepath.

i wrote code without regex, it's messy, uses strncmp, 2 loops , bools validate request. still can't figure out how verify http/1.1 though. here's code (note - "line" char array stores http request)

// validate request-line         if (strncmp(line, "get", 3) != 0)             error(405);         if (line[3] != ' ')             error(400);         if (line[4] != '/')             error(501);          // initializing variable future use         bool period = false;         int y = strlen(line);         bool secondspace = false;          // quotes not allowed         (int z = 0; z < y; z++)         {             if (line[z] == '"')                 error(400);         }          // check period in file path         (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); 

you can use pcre c-api. (latest 10.10)

download page: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

i show own 'preg_match' function use api:

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); } 

and can use in c program way:

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); } 

output:

file: foo.html


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -