Search code examples
chttphttp-request

More optimal way to find a specific word in a string buffers C


I've made an app that parses HTTP headers. I'm trying to find if there is a better way to filter HTTP packets by POST method than the one I've come up with. What I am trying to accomplish is to take advantage of the fact that I know that all the POST methods packet strings start with "POST". Is there a way to search for the first word of a string, store it and then use a condition with it? My code works but I would prefer not to search for the whole packet for a "POST" - you never know when you get the word "POST" inside a GET packet, for example.

   char re[size_data];
   strncpy(re,data,size_data);   //data is the buffer and size_data the buffer size
   char * check;
   check = strstr(re,"POST"); 
   if(check!= NULL)
  { *something happens* }

Solution

  • Since you only want to check for the string "POST" at the beginning of the packet, you can use the strncmp function, e.g.

    if ( strncmp( re, "POST ", 5 ) == 0 )
    {
        // this is a POST packet
    }
    

    As noted by @jxh in the comments, the strncpy may cause problems, since it won't null terminate the string unless the string length is less than size_data. To make sure the string is properly terminated, the code should look like this

    char re[size_data+1];
    strncpy(re,data,size_data);
    re[size_data] = '\0';