Exemple #1
0
int
nni_http_res_set_data(nni_http_res *res, const void *data, size_t size)
{
	int rv;

	http_entity_set_data(&res->data, data, size);
	if ((rv = http_set_content_length(&res->data, &res->hdrs)) != 0) {
		http_entity_set_data(&res->data, NULL, 0);
	}
	return (rv);
}
Exemple #2
0
int
nni_http_req_copy_data(nni_http_req *req, const void *data, size_t size)
{
	int rv;

	if (((rv = http_entity_copy_data(&req->data, data, size)) != 0) ||
	    ((rv = http_set_content_length(&req->data, &req->hdrs)) != 0)) {
		http_entity_set_data(&req->data, NULL, 0);
		return (rv);
	}
	return (0);
}
Exemple #3
0
int
http_send_document(http_t *ctx, const char *data, size_t size)
{
  RUNTIME_ASSERT(ctx != NULL);

  if (!ctx->status_code) {
    ctx->status_code = 200;
    ctx->status_msg = "OK";
  }
  http_send_status_line(ctx, ctx->status_code, ctx->status_msg);
  http_set_content_length(ctx, size);
  http_send_content_length_header(ctx);
  http_send_line(ctx, http_connection_header(ctx));
  http_send_extra_headers(ctx);
  ACCLOG_SET(ctx->acclog, size, size);
  http_terminate_headers(ctx);

  if (size > 0)
    http_send_data(ctx, data, size);
  
  return 0;
}
Exemple #4
0
GString* http_post_stream_upload(http* h, const gchar* url, goffset len, http_data_fn read_cb, gpointer user_data, GError** err)
{
  struct curl_slist* headers = NULL;
  glong http_status = 0;
  GString* response;
  CURLcode res;
  struct _stream_data data;

  g_return_val_if_fail(h != NULL, NULL);
  g_return_val_if_fail(url != NULL, NULL);
  g_return_val_if_fail(err == NULL || *err == NULL, NULL);

  http_no_expect(h);

  // setup post headers and url
  curl_easy_setopt(h->curl, CURLOPT_POST, 1L);
  curl_easy_setopt(h->curl, CURLOPT_URL, url);

  // setup request post body writer
  http_set_content_length(h, len);
  curl_easy_setopt(h->curl, CURLOPT_POSTFIELDSIZE_LARGE, len);

  data.cb = read_cb;
  data.user_data = user_data;
  curl_easy_setopt(h->curl, CURLOPT_READFUNCTION, (curl_read_callback)curl_read);
  curl_easy_setopt(h->curl, CURLOPT_READDATA, &data);

  // prepare buffer for the response body
  response = g_string_sized_new(512);
  curl_easy_setopt(h->curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)append_gstring);
  curl_easy_setopt(h->curl, CURLOPT_WRITEDATA, response);

  g_hash_table_foreach(h->headers, (GHFunc)add_header, &headers);
  curl_easy_setopt(h->curl, CURLOPT_HTTPHEADER, headers);

  // perform HTTP request
  res = curl_easy_perform(h->curl);

  // check the result
  if (res == CURLE_OK)
  {
    if (curl_easy_getinfo(h->curl, CURLINFO_RESPONSE_CODE, &http_status) == CURLE_OK)
    {
      if (http_status == 200)
      {
        goto out;
      }
      else
      {
        g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "Server returned %ld", http_status);
      }
    }
    else
    {
      g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "Can't get http status code");
    }
  }
  else if (res == CURLE_GOT_NOTHING)
  {
    g_set_error(err, HTTP_ERROR, HTTP_ERROR_NO_RESPONSE, "CURL error: %s", curl_easy_strerror(res));
  }
  else
  {
    g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "CURL error: %s", curl_easy_strerror(res));
  }

  g_string_free(response, TRUE);
  response = NULL;

out:
  curl_easy_setopt(h->curl, CURLOPT_HTTPHEADER, NULL);
  curl_slist_free_all(headers);
  if (response)
    return response;

  return NULL;
}