/** * Creates a new #SteamHttpReq. The returned #SteamHttpReq should be * freed with #steam_http_req_free() when no longer needed. * * @param http The #SteamHttp. * @param host The hostname. * @param port The port number. * @param path The pathname. * @param func The user callback function or NULL. * @param data The user define data or NULL. * * @return The #SteamHttpReq or NULL on error. **/ SteamHttpReq *steam_http_req_new(SteamHttp *http, const gchar *host, gint port, const gchar *path, SteamHttpFunc func, gpointer data) { SteamHttpReq *req; req = g_new0(SteamHttpReq, 1); req->http = http; req->host = g_strdup(host); req->port = port; req->path = g_strdup(path); req->func = func; req->data = data; req->headers = g_hash_table_new_full(g_str_hash, (GEqualFunc) steam_util_str_iequal, g_free, g_free); req->params = g_hash_table_new_full(g_str_hash, (GEqualFunc) steam_util_str_iequal, g_free, g_free); steam_http_req_headers_set(req, STEAM_HTTP_PAIR("User-Agent", http->agent), STEAM_HTTP_PAIR("Host", host), STEAM_HTTP_PAIR("Accept", "*/*"), STEAM_HTTP_PAIR("Connection", "Close"), NULL ); return req; }
/** * Assembles a #SteamHttpReq. The returned strings should be freed * with #g_free() when no longer needed. * * @param req The #SteamHttpReq. * @param hs The return location for the header string. * @param ps The return location for the param string. * @param fs The return location for the full string. **/ static void steam_http_req_asm(SteamHttpReq *req, gchar **hs, gchar **ps, gchar **fs) { GHashTableIter iter; GString *hgs; GString *pgs; gchar *str; gchar *key; gchar *val; g_hash_table_iter_init(&iter, req->params); pgs = g_string_sized_new(128); while (g_hash_table_iter_next(&iter, (gpointer*) &key, (gpointer*) &val)) { if (val == NULL) val = ""; key = steam_http_uri_escape(key); val = steam_http_uri_escape(val); str = (pgs->len > 0) ? "&" : ""; g_string_append_printf(pgs, "%s%s=%s", str, key, val); g_free(key); g_free(val); } if (g_hash_table_size(req->http->cookies) > 0) { str = steam_http_cookies_str(req->http); steam_http_req_headers_set(req, STEAM_HTTP_PAIR("Cookie", str), NULL); g_free(str); } if (req->flags & STEAM_HTTP_REQ_FLAG_POST) { str = g_strdup_printf("%" G_GSIZE_FORMAT, pgs->len); steam_http_req_headers_set(req, STEAM_HTTP_PAIR("Content-Type", "application/" "x-www-form-urlencoded"), STEAM_HTTP_PAIR("Content-Length", str), NULL ); g_free(str); } g_hash_table_iter_init(&iter, req->headers); hgs = g_string_sized_new(128); while (g_hash_table_iter_next(&iter, (gpointer*) &key, (gpointer*) &val)) { if (val == NULL) val = ""; g_string_append_printf(hgs, "%s: %s\r\n", key, val); } if (req->flags & STEAM_HTTP_REQ_FLAG_POST) { *fs = g_strdup_printf("POST %s HTTP/1.1\r\n%s\r\n%s", req->path, hgs->str, pgs->str); } else { *fs = g_strdup_printf("GET %s?%s HTTP/1.1\r\n%s\r\n", req->path, pgs->str, hgs->str); } *hs = g_string_free(hgs, FALSE); *ps = g_string_free(pgs, FALSE); }