static gboolean do_send_headers(MegaHttpClient* http_client, GCancellable* cancellable, GError** err) { GError* local_err = NULL; GString* headers; g_return_val_if_fail(MEGA_IS_HTTP_CLIENT(http_client), FALSE); g_return_val_if_fail(err == NULL || *err == NULL, FALSE); MegaHttpClientPrivate* priv = http_client->priv; headers = g_string_sized_new(300); mega_http_client_set_header(http_client, "Host", priv->host); mega_http_client_set_content_length(http_client, priv->expected_write_count); g_string_append_printf(headers, "%s %s HTTP/1.1\r\n", "POST", priv->resource); g_hash_table_foreach(priv->request_headers, (GHFunc)add_header, headers); g_string_append(headers, "\r\n"); gboolean rs = g_output_stream_write_all(priv->ostream, headers->str, headers->len, NULL, cancellable, &local_err); if (!rs) { g_set_error(err, MEGA_HTTP_CLIENT_ERROR, MEGA_HTTP_CLIENT_ERROR_CONNECTION_BROKEN, "Can't write request headers: %s", local_err ? local_err->message : "unknown error"); g_clear_error(&local_err); } g_string_free(headers, TRUE); return rs; }
/** * mega_http_client_set_content_type: * @http_client: a #MegaHttpClient * @content_type: Content type. * * Set content type header. */ void mega_http_client_set_content_type(MegaHttpClient* http_client, const gchar* content_type) { g_return_if_fail(MEGA_IS_HTTP_CLIENT(http_client)); g_return_if_fail(content_type != NULL); mega_http_client_set_header(http_client, "Content-Type", content_type); }
/** * mega_http_client_set_content_length: * @http_client: a #MegaHttpClient * @content_length: Content length. * * Set content length header. */ void mega_http_client_set_content_length(MegaHttpClient* http_client, guint64 content_length) { g_return_if_fail(MEGA_IS_HTTP_CLIENT(http_client)); gchar* tmp = g_strdup_printf("%" G_GUINT64_FORMAT, content_length); mega_http_client_set_header(http_client, "Content-Length", tmp); g_free(tmp); }
static void mega_http_client_init(MegaHttpClient *http_client) { MegaHttpClientPrivate* priv = http_client->priv = G_TYPE_INSTANCE_GET_PRIVATE(http_client, MEGA_TYPE_HTTP_CLIENT, MegaHttpClientPrivate); priv->client = g_socket_client_new(); g_socket_client_set_timeout(priv->client, 60); g_socket_client_set_family(priv->client, G_SOCKET_FAMILY_IPV4); priv->request_headers = g_hash_table_new_full(stri_hash, stri_equal, g_free, g_free); priv->response_headers = g_hash_table_new_full(stri_hash, stri_equal, g_free, g_free); priv->regex_url = g_regex_new("^([a-z]+)://([a-z0-9.-]+(?::([0-9]+))?)(/.+)?$", G_REGEX_CASELESS, 0, NULL); priv->regex_status = g_regex_new("^HTTP/([0-9]+\\.[0-9]+) ([0-9]+) (.+)$", 0, 0, NULL); // set default headers mega_http_client_set_header(http_client, "Referer", "https://mega.co.nz/"); mega_http_client_set_header(http_client, "User-Agent", "Megatools (" VERSION ")"); mega_http_client_set_header(http_client, "Connection", "keep-alive"); }