Merge from amd64 branch:
[reactos.git] / rosapps / applications / sysutils / rosddt / http.c
1 #include <windows.h>
2 #include <wininet.h>
3 #include <urlmon.h>
4 #include <stdio.h>
5 #include "http.h"
6
7 #define INTERNET_FLAGS (INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_NO_COOKIES )
8
9 static char *http_receive(HINTERNET h_req, u_long *d_size)
10 {
11 u_long bytes = sizeof(u_long);
12 u_long qsize = 0;
13 u_long readed = 0;
14 char *data = NULL;
15 char buff[4096];
16
17 if (HttpQueryInfo(h_req, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &qsize, &bytes, NULL) != 0) {
18 data = malloc(qsize + 1);
19 }
20
21 do
22 {
23 if (InternetReadFile(h_req, buff, sizeof(buff), &bytes) == 0) {
24 break;
25 }
26 if ( (readed + bytes) > qsize) {
27 data = realloc(data, readed + bytes + 1);
28 if (data == NULL) break;
29 qsize += bytes;
30 }
31 memcpy(data + readed, buff, bytes); readed += bytes;
32 } while (bytes != 0);
33
34 if ( (data != NULL) && (readed != qsize) ) {
35 free(data); data = NULL;
36 } else {
37 if (d_size != NULL) *d_size = readed;
38 data[readed] = 0;
39 }
40 return data;
41 }
42
43 void *http_get(wchar_t *url, u_long *d_size)
44 {
45 HINTERNET h_inet = NULL;
46 HINTERNET h_req = NULL;
47 char *replay = NULL;
48
49 do
50 {
51 h_inet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
52 if (h_inet == NULL) break;
53
54 h_req = InternetOpenUrl(h_inet, url, NULL, 0, INTERNET_FLAGS, 0);
55 if (h_req == NULL) break;
56
57 replay = http_receive(h_req, d_size);
58 } while (0);
59
60 if (h_req != NULL) {
61 InternetCloseHandle(h_req);
62 }
63 if (h_inet != NULL) {
64 InternetCloseHandle(h_inet);
65 }
66 return replay;
67 }
68
69 void *http_post(wchar_t *url, void *data, int size, u_long *d_size)
70 {
71 URL_COMPONENTS url_cm = {0};
72 HINTERNET h_inet = NULL;
73 HINTERNET h_conn = NULL;
74 HINTERNET h_req = NULL;
75 char *q_data = NULL;
76 char *replay = NULL;
77 wchar_t host[MAX_PATH];
78 wchar_t path[MAX_PATH];
79 unsigned char *p, *d = data;
80
81 do
82 {
83 if ( (q_data = malloc(size * 3 + 10)) == NULL ) {
84 break;
85 }
86 strcpy(q_data, "data="); p = q_data + 5;
87
88 while (size--) {
89 p += sprintf(p, "%%%0.2x", (u_long)*d++);
90 }
91
92 url_cm.dwStructSize = sizeof(url_cm);
93 url_cm.lpszHostName = host;
94 url_cm.dwHostNameLength = sizeof(host);
95 url_cm.lpszUrlPath = path;
96 url_cm.dwUrlPathLength = sizeof(path);
97
98 if (InternetCrackUrl(url, 0, 0, &url_cm) == 0) {
99 break;
100 }
101
102 h_inet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
103 if (h_inet == NULL) break;
104
105 h_conn = InternetConnect(h_inet, host, url_cm.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
106 if (h_conn == NULL) break;
107
108 h_req = HttpOpenRequest(h_conn, L"POST", path, NULL, NULL, NULL, INTERNET_FLAGS, 0);
109 if (h_req == NULL) break;
110
111 HttpAddRequestHeaders(
112 h_req, L"Content-Type: application/x-www-form-urlencoded", 47*2, HTTP_ADDREQ_FLAG_ADD);
113
114 if (HttpSendRequest(h_req, NULL, 0, q_data, strlen(q_data)) == 0) {
115 break;
116 }
117 replay = http_receive(h_req, d_size);
118 } while (0);
119
120 if (h_req != NULL) {
121 InternetCloseHandle(h_req);
122 }
123 if (h_conn != NULL) {
124 InternetCloseHandle(h_conn);
125 }
126 if (h_inet != NULL) {
127 InternetCloseHandle(h_inet);
128 }
129 if (q_data != NULL) free(q_data);
130 return replay;
131 }