#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char * argv[]) {
  char buf[3000];
  FILE * f = fopen("request_headers.txt", "r");
  if (f == NULL) { perror("request_headers.txt"); exit(0); }
  
  int numBytes = fread(buf, 1, 2999, f);
  if (numBytes <= 0) exit(0);
  buf[numBytes] = '\n';
  //printf("%s\n", buf);

  // parse the string into lines
  // look for "\n"
  char *needle = "\n";
  char *curr_line, *next_line;
  curr_line = buf;

  // find the end of the line
  char *tmp = strstr(buf, needle);
  
  while (tmp != NULL) { // 
    // put NULL, so curr_line is just the first line
    *tmp = '\0';

    // do something with the line
    printf("line starts with %c, and is length %d\n", *curr_line, strlen(curr_line));

    /*
      separate into field name and field value...

      char * colon = strstr(curr_line+1, ":");
      *colon = '\0';
      char *field_name = curr_line;
      char *field_value = colon+1;
      while (isspace(*field_value)) field_value++;

      if (strcmp(field_name, "Date") == 0) {
        // field_value is the date
      }
     */
    
    // update some variable, so go the next line
    // update curr_line to be the next line
    curr_line = tmp+1;

    // and find next \n
    tmp = strstr(curr_line, needle);
  }

  // there wasn't another newline
}
