c0e6fd8a1d477d8e802393b559bbd02410cd0e6f
[reactos.git] / modules / 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 char *p = NULL;
80 char *d = data;
81
82 do
83 {
84 if ( (q_data = malloc(size * 3 + 10)) == NULL ) {
85 break;
86 }
87 strcpy(q_data, "data="); p = q_data + 5;
88
89 while (size--) {
90 p += sprintf(p, "%%%.2x", (u_int)*d++);
91 }
92
93 url_cm.dwStructSize = sizeof(url_cm);
94 url_cm.lpszHostName = host;
95 url_cm.dwHostNameLength = sizeof(host);
96 url_cm.lpszUrlPath = path;
97 url_cm.dwUrlPathLength = sizeof(path);
98
99 if (InternetCrackUrl(url, 0, 0, &url_cm) == 0) {
100 break;
101 }
102
103 h_inet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
104 if (h_inet == NULL) break;
105
106 h_conn = InternetConnect(h_inet, host, url_cm.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
107 if (h_conn == NULL) break;
108
109 h_req = HttpOpenRequest(h_conn, L"POST", path, NULL, NULL, NULL, INTERNET_FLAGS, 0);
110 if (h_req == NULL) break;
111
112 HttpAddRequestHeaders(
113 h_req, L"Content-Type: application/x-www-form-urlencoded", 47*2, HTTP_ADDREQ_FLAG_ADD);
114
115 if (HttpSendRequest(h_req, NULL, 0, q_data, strlen(q_data)) == 0) {
116 break;
117 }
118 replay = http_receive(h_req, d_size);
119 } while (0);
120
121 if (h_req != NULL) {
122 InternetCloseHandle(h_req);
123 }
124 if (h_conn != NULL) {
125 InternetCloseHandle(h_conn);
126 }
127 if (h_inet != NULL) {
128 InternetCloseHandle(h_inet);
129 }
130 if (q_data != NULL) free(q_data);
131 return replay;
132 }