title: Receive http response messages using OpenSSL in C
body: <p>I have some problems when trying to receive http response message of a website.<br>
  This is my function:</p>
  
  <pre><code>void Reveive_response(char *resp, SSL *ssl) {
  
      const int BUFFER_SIZE = 1024;
      char response[1048576];
      char *buffer = NULL;            // to read from ssl
      char *check = (char *) malloc(BUFFER_SIZE*sizeof(char));
      int bytes;                      // number of bytes actually read
      int received = 0;               // number of bytes received
  
      buffer = (char *) malloc(BUFFER_SIZE*sizeof(char));     // malloc
      memset(response, '\0', sizeof(response));               // response
      assign = '\0'
      do{
          memset(buffer, '\0', BUFFER_SIZE);          // empty buffer
          bytes = SSL_read(ssl, buffer, BUFFER_SIZE);
          if (bytes &lt; 0) {
              printf("Error: Receive response\n");
              exit(0);
          }
          if (bytes == 0) break;
          received += bytes;
          printf("Received...%d bytes\n", received);
          strncat(response, buffer, bytes);   // concat buffer to response
      } while (SSL_pending(ssl));             // while pending
      response[received] = '\0';
      printf("Receive DONE\n");
      printf("Response: \n%s\n", response);
      free(buffer);
      strcpy(resp, response);                 // return via resp
  
  }
  </code></pre>
  
  <p>When I call the function, it seems like the response message is not complete. Like this:</p>
  
  <pre><code>Received...1014 bytes
  Received...1071 bytes
  Receive DONE
  Response: 
  HTTP/1.1 200 OK
  &lt;... something else....&gt;
  Vary: Accept-Encoding
  Content-Type: text/html
  Conne
  </code></pre>
  
  <p>Then if i call the function again, it returns:</p>
  
  <pre><code>Received...39 bytes
  Receive DONE
  Response:
  ction: keep-alive
  Content-Length: 0
  </code></pre>
  
  <p>The field Connection was split. Why my function didn't receive all the response message? I used do while loop inside. Please tell me where did i go wrong? Thank you.</p>