fork download
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void) {
  6. char* msg = "HTTP/1.1 200 OK\r\n"
  7. "Date: Sat, 04 Jun 2016 13:07:05 GMT\r\n"
  8. "Server: Apache\r\n"
  9. "X-Powered-By: PHP/5.5.35\r\n"
  10. "Content-Length: 2\r\n"
  11. "Connection: close\r\n"
  12. "Content-Type: text/html\r\n"
  13. "\r\n"
  14. "11\r\n";
  15.  
  16. // lookup the content-length header
  17. char* content = strstr(msg, "Content-Length: ");
  18. // skip the looked up string:
  19. content = content+strlen("Content-Length: ");
  20. // store the size of the contents in number of characters
  21. int nb_chars = atoi(content);
  22. // lookup the content
  23. content = strstr(msg, "\r\n\r\n");
  24. // check that the substring has been found
  25. if (content == NULL) {
  26. printf("Couldn't find the contents within the message!");
  27. } else {
  28. // skip the header-content delimiter
  29. content = content+4;
  30. // and do useful things with the extracted data
  31. if (nb_chars == 2) {
  32. printf("The content is:");
  33. printf("%s\n", content);
  34. } else {
  35. printf("There's been an error processing the request.");
  36. }
  37. }
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
The content is:11