title: How can I most effectively parse this HTTP request in C?
body: <p>The information I really need to extract is:</p>
  
  <p>a) Whether or not it is a <code>GET</code> request</p>
  
  <p>b) The file address (e.g. index.html)</p>
  
  <p>c) The host information (e.g. localhost:8081)</p>
  
  <p>I have code to do this just now (see bottom of my post), but it seems inefficient, quite static, and doesn't pull the host information.</p>
  
  <p>So I'd like to have a <em>sane</em> solution to parsing the HTTP request in C. Cheers!</p>
  
  <p><strong>HTTP Request</strong></p>
  
  <pre><code>GET /index.html HTTP/1.1
  Host: localhost:8081
  Connection: keep-alive
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.70 Safari/537.17
  DNT: 1
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: en-US,en;q=0.8,en-GB;q=0.6
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  </code></pre>
  
  <p><strong>Current Code</strong></p>
  
  <pre><code>int parsehttp(char *inputstring, int *type, char *getaddress) {
      if((strncmp(inputstring, "GET", 3)) == 0) {
          *type = 1;
      } else {
          *type = 0;
      }
      char firstline[BUFLEN] = "";
      int charoffset = getlineend(inputstring); //this function returns the int offset of '\r\n'
      strncpy(firstline, inputstring, charoffset-2);
      firstline[charoffset-1] = '\0';
      sscanf(firstline,"%*s %s %*s",getaddress);
      inputstring = (inputstring + charoffset);
      return 1;
  }
  </code></pre>