title: How do I send an image over HTTP protocol in C?
body: <p>I'm a student doing a web server exercise and I need a bit of help.</p>
  
  <p>I have my web server working fine for text pages but whenever the brower sends a <code>---GET /img.jpg HTTP/1.1</code> request, I don't know how to handle it. I've heard the HTTP protocol is text based, so how do I send an image in my HTTP response?</p>
  
  <p>Here is a segment where I create my normal HTTP response, I plan to use <code>readresult == 2</code> to signal an image.</p>
  
  <pre><code>if(readresult == 1){
      sprintf(toreturn, "%s\r\n%s\r\n%s\r\n\r\n%s", "HTTP/1.1 200 OK", "Content-Type: text/html", "Connection: close", readpagestring);
      returnflag = 1;
  }
  else if(readresult == 2){
      returnflag = 2;
  }
  else{
      sprintf(toreturn, "%s\r\n%s\r\n%s\r\n\r\n%s", "HTTP/1.1 404 Not Found", "Content-Type: text/html", "Connection: close", readpagestring);
      returnflag = 0;
  }
  </code></pre>
  
  <p>And the function it calls</p>
  
  <pre><code>int readpage(char *readaddress, char *pagereturn){
      FILE *inputfile = (FILE *)calloc(1,sizeof(FILE));
      int flag;
      int c;
      int n = 0;
      readaddress++;
      inputfile=fopen(readaddress,"r");
      if (inputfile==NULL){
          FILE *missingfile;
          missingfile=fopen("404.html","r");
          while ((c = fgetc (missingfile)) != EOF){
              *(pagereturn+n) = c;
              n++;
          }
          flag = 0;
          fclose (missingfile);
      }
      else{
          while ((c = fgetc (inputfile)) != EOF){
              *(pagereturn+n) = c;
              n++;
          }
          flag = 1;
          fclose (inputfile);
      }
      return flag;
  }
  </code></pre>