88
99#include < config.h>
1010
11+ #include < microhttpd.h>
12+ #include < curl/curl.h>
1113#include < cstring>
1214#include < string>
1315#include < vector>
1416#include < sstream>
15- #include < microhttpd.h>
16- #include < curl/curl.h>
1717
1818constexpr int BUFFER_SIZE = 128 ;
1919static const char *g_path = nullptr ;
@@ -43,6 +43,20 @@ static MHD_Result get_cookies(void *cls, enum MHD_ValueKind kind, const char *ke
4343 return MHD_YES;
4444}
4545
46+ // reads the microhttpd request URL args
47+ static MHD_Result get_url_arg (void *cls, enum MHD_ValueKind kind, const char *key, const char *value) {
48+ auto *args = static_cast <string*>(cls);
49+ if (key && value) {
50+ if (!(*args).empty ()) {
51+ *args += " &" ;
52+ }
53+ *args += key;
54+ *args += " =" ;
55+ *args += value;
56+ }
57+ return MHD_YES;
58+ }
59+
4660// copy the curl proxy cookies back to the microhttpd response
4761static void set_cookies (MHD_Response *response, curl_slist *cookies) {
4862 // assumes cookies use the netscape 7 field tab separated format
@@ -108,6 +122,12 @@ MHD_Response *proxy_request(MHD_Connection *connection, const char *path, const
108122 string url;
109123 url.append (g_host).append (" /" ).append (path);
110124
125+ string url_args;
126+ MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, get_url_arg, &url_args);
127+ if (!url_args.empty ()) {
128+ url.append (" ?" ).append (url_args);
129+ }
130+
111131 string response;
112132 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_callback);
113133 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &response);
@@ -131,6 +151,9 @@ MHD_Response *proxy_request(MHD_Connection *connection, const char *path, const
131151 set_cookies (result, receiveCookies);
132152 }
133153
154+ if (receiveCookies) {
155+ curl_slist_free_all (receiveCookies);
156+ }
134157 return result;
135158}
136159
0 commit comments