Ejemplo n.º 1
0
/*2015.10.14  Modify by wolf-lone*/
static void flush_response_body(http_resp *a_resp, http_trans_conn *a_conn)
{
	ghttpDebug("io_buf_alloc: %d; body_len: %d; tmpbody_len: %d \n", a_conn->io_buf_alloc, a_resp->body_len, a_resp->tmpbody_len);
	count_prev_body_len(a_resp);

	if(a_resp->tmpbody){
		a_resp->body = a_resp->tmpbody;
		a_resp->body_len = a_resp->tmpbody_len;
		// don't free tmpbody here.  free tmpbody at next free body
		a_resp->tmpbody = NULL;
		a_resp->tmpbody_len = 0;
		ghttpDebug("flushed_length=%d, body_len=%d, io_buf_alloc=%d \n", a_resp->flushed_length, a_resp->body_len, a_conn->io_buf_alloc);
		return;
	}
	
	if(a_conn->io_buf_alloc <= 0)
		return;

	// io_buf --> body
	ghttpDebug("flushed_length=%d, body_len=%d, io_buf_alloc=%d \n", a_resp->flushed_length, a_resp->body_len, a_conn->io_buf_alloc);
	a_resp->body_len = a_conn->io_buf_alloc;
	a_resp->body = malloc(a_conn->io_buf_alloc + 1);
	memset(a_resp->body, 0, a_conn->io_buf_alloc + 1);
	memcpy(a_resp->body, a_conn->io_buf, a_conn->io_buf_alloc);
	/* clean the buffer */
	http_trans_buf_reset(a_conn);
	ghttpDebug("flushed_length=%d, body_len=%d, io_buf_alloc=%d \n", a_resp->flushed_length, a_resp->body_len, a_conn->io_buf_alloc);
}
Ejemplo n.º 2
0
void
ghttp_clean(ghttp_request *a_request)
{
  http_resp_destroy(a_request->resp);
  a_request->resp = http_resp_new();
  http_req_destroy(a_request->req);
  a_request->req = http_req_new();
  http_trans_buf_reset(a_request->conn);
  a_request->proc = ghttp_proc_none;
  return;
}
Ejemplo n.º 3
0
static void
flush_response_body(http_resp *a_resp,
                    http_trans_conn *a_conn)
{
  if (a_resp->body != NULL) {
    free(a_resp->body);
  }
  a_resp->flushed_length += a_resp->body_len;
  a_resp->body_len = a_conn->io_buf_alloc;
  a_resp->body = malloc(a_conn->io_buf_alloc + 1);
  memset(a_resp->body, 0, a_conn->io_buf_alloc + 1);
  memcpy(a_resp->body, a_conn->io_buf, a_conn->io_buf_alloc);
  /* clean the buffer */
  http_trans_buf_reset(a_conn);
}
Ejemplo n.º 4
0
static void set_tmpbody(http_resp *a_resp, http_trans_conn *a_conn, int len)
{
	if(len >= a_conn->io_buf_alloc)
		len = a_conn->io_buf_alloc;
	
	if(a_resp->tmpbody){
		a_resp->tmpbody = realloc(a_resp->tmpbody, a_resp->tmpbody_len + len + 1);
		a_resp->tmpbody_len += len;
		memset(a_resp->tmpbody + a_resp->tmpbody_len, 0, len+1);
		memcpy(a_resp->tmpbody + a_resp->tmpbody_len, a_conn->io_buf, len);
	}
	else{
		a_resp->tmpbody = (char *)malloc(len+1);
		a_resp->tmpbody_len = len;
		memset(a_resp->tmpbody, 0, len+1);
		memcpy(a_resp->tmpbody, a_conn->io_buf, len);
	}
	ghttpDebug("[set_tmpbody]  %d \n", a_resp->tmpbody_len);
	if(len == a_conn->io_buf_alloc)
		http_trans_buf_reset(a_conn);
	else
		http_trans_buf_clip(a_conn, a_conn->io_buf + len);
}
Ejemplo n.º 5
0
int
http_req_send(http_req *a_req, http_trans_conn *a_conn)
{
  char       *l_request = NULL;
  int         l_request_len = 0;
  int         i = 0;
  int         l_len = 0;
  int         l_headers_len = 0;
  int         l_rv = 0;
  char       *l_content = NULL;

  /* see if we need to jump into the function somewhere */
  if (a_conn->sync == HTTP_TRANS_ASYNC)
    {
      if (a_req->state == http_req_state_sending_request)
	goto http_req_state_sending_request_jump;
      if (a_req->state == http_req_state_sending_headers)
	goto http_req_state_sending_headers_jump;
      if (a_req->state == http_req_state_sending_body)
	goto http_req_state_sending_body_jump;
    }
  /* enough for the request and the other little headers */
  l_request = malloc(30 + strlen(a_req->resource) + (a_conn->proxy_host ?
						     (strlen(a_req->host) + 20) : 0));
  memset(l_request, 0, 30 + strlen(a_req->resource) + (a_conn->proxy_host ?
						       (strlen(a_req->host) + 20) : 0));
  /* copy it into the buffer */
  if (a_conn->proxy_host)
    {
      l_request_len = sprintf(l_request,
			      "%s %s HTTP/%01.1f\r\n",
			      http_req_type_char[a_req->type],
			      a_req->full_uri,
			      a_req->http_ver);
    }
  else
    {
      l_request_len = sprintf(l_request,
			      "%s %s HTTP/%01.1f\r\n",
			      http_req_type_char[a_req->type],
			      a_req->resource,
			      a_req->http_ver);
    }
  /* set the request in the connection buffer */
  http_trans_append_data_to_buf(a_conn, l_request, l_request_len);
  /* free up the request - we don't need it anymore */
  free(l_request);
  l_request = NULL;
  /* set the state */
  a_req->state = http_req_state_sending_request;
 http_req_state_sending_request_jump:
  /* send the request */
  do {
    l_rv = http_trans_write_buf(a_conn);
    if ((a_conn->sync == HTTP_TRANS_ASYNC) && (l_rv == HTTP_TRANS_NOT_DONE))
      return HTTP_TRANS_NOT_DONE;
    if ((l_rv == HTTP_TRANS_DONE) && (a_conn->last_read == 0))
      return HTTP_TRANS_ERR;
  } while (l_rv == HTTP_TRANS_NOT_DONE);
  /* reset the buffer */
  http_trans_buf_reset(a_conn);
  /* set up all of the headers */
  for (i = 0; i < HTTP_HDRS_MAX; i++)
    {
      l_len = 0;
      if (a_req->headers->header[i])
	{
	  l_len = strlen(a_req->headers->header[i]);
	  if (l_len > 0)
	    {
	      http_trans_append_data_to_buf(a_conn, a_req->headers->header[i], l_len);
	      l_headers_len += l_len;
	      http_trans_append_data_to_buf(a_conn, ": ", 2);
	      l_headers_len += 2;
	      /* note, it's ok to have no value for a request */
	      if ((l_len = strlen(a_req->headers->value[i])) > 0)
		{
		  http_trans_append_data_to_buf(a_conn, a_req->headers->value[i], l_len);
		  l_headers_len += l_len;
		}
	      http_trans_append_data_to_buf(a_conn, "\r\n", 2);
	      l_headers_len += 2;
	    }
	}
    }
  http_trans_append_data_to_buf(a_conn, "\r\n", 2);
  l_headers_len += 2;
  /* set the state */
  a_req->state = http_req_state_sending_headers;
 http_req_state_sending_headers_jump:
  /* blast that out to the network */
  do {
    l_rv = http_trans_write_buf(a_conn);
    if ((a_conn->sync == HTTP_TRANS_ASYNC) && (l_rv == HTTP_TRANS_NOT_DONE))
      return HTTP_TRANS_NOT_DONE;
    if ((l_rv == HTTP_TRANS_DONE) && (a_conn->last_read == 0))
      return HTTP_TRANS_ERR;
  } while (l_rv == HTTP_TRANS_NOT_DONE);
  /* reset the buffer */
  http_trans_buf_reset(a_conn);
  l_content = http_hdr_get_value(a_req->headers, http_hdr_Content_Length);
  if (l_content)
    {
      /* append the information to the buffer */
      http_trans_append_data_to_buf(a_conn, a_req->body, a_req->body_len);
      a_req->state = http_req_state_sending_body;
    http_req_state_sending_body_jump:
      do {
	l_rv = http_trans_write_buf(a_conn);
	if ((a_conn->sync == HTTP_TRANS_ASYNC) && (l_rv == HTTP_TRANS_NOT_DONE))
	  return HTTP_TRANS_NOT_DONE;
	if ((l_rv == HTTP_TRANS_DONE) && (a_conn->last_read == 0))
	  return HTTP_TRANS_ERR;
      } while (l_rv == HTTP_TRANS_NOT_DONE);
      /* reset the buffer */
      http_trans_buf_reset(a_conn);
    }
  return HTTP_TRANS_DONE;
}