send.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * HTTP handling functions.
  3. *
  4. * Copyright 2003 Ferenc Wagner
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <winsock2.h>
  21. #include <wininet.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include "winetest.h"
  25. #define USER_AGENT "Winetest Shell"
  26. #define SERVER_NAME "test.winehq.org"
  27. #define URL_PATH "/submit"
  28. #define SEP "--8<--cut-here--8<--"
  29. #define CONTENT_HEADERS "Content-Type: multipart/form-data; boundary=\"" SEP "\"\r\n" \
  30. "Content-Length: %u\r\n\r\n"
  31. static const char body1[] = "--" SEP "\r\n"
  32. "Content-Disposition: form-data; name=\"reportfile\"; filename=\"%s\"\r\n"
  33. "Content-Type: application/octet-stream\r\n\r\n";
  34. static const char body2[] = "\r\n--" SEP "\r\n"
  35. "Content-Disposition: form-data; name=\"submit\"\r\n\r\n"
  36. "Upload File\r\n"
  37. "--" SEP "--\r\n";
  38. static SOCKET
  39. open_http (const char *server)
  40. {
  41. WSADATA wsad;
  42. struct sockaddr_in sa;
  43. SOCKET s;
  44. report (R_STATUS, "Opening HTTP connection to %s", server);
  45. if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET;
  46. sa.sin_family = AF_INET;
  47. sa.sin_port = htons (80);
  48. sa.sin_addr.s_addr = inet_addr (server);
  49. memset (sa.sin_zero, 0, sizeof(sa.sin_zero));
  50. if (sa.sin_addr.s_addr == INADDR_NONE) {
  51. struct hostent *host = gethostbyname (server);
  52. if (!host) {
  53. report (R_ERROR, "Hostname lookup failed for %s", server);
  54. goto failure;
  55. }
  56. sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
  57. }
  58. s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  59. if (s == INVALID_SOCKET) {
  60. report (R_ERROR, "Can't open network socket: %d",
  61. WSAGetLastError ());
  62. goto failure;
  63. }
  64. if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in)))
  65. return s;
  66. report (R_ERROR, "Can't connect: %d", WSAGetLastError ());
  67. closesocket (s);
  68. failure:
  69. WSACleanup ();
  70. return INVALID_SOCKET;
  71. }
  72. static int
  73. close_http (SOCKET s)
  74. {
  75. int ret;
  76. ret = closesocket (s);
  77. return (WSACleanup () || ret);
  78. }
  79. static int
  80. send_buf (SOCKET s, const char *buf, size_t length)
  81. {
  82. int sent;
  83. while (length > 0) {
  84. sent = send (s, buf, length, 0);
  85. if (sent == SOCKET_ERROR) {
  86. if (errno == EINTR) continue;
  87. return 1;
  88. }
  89. buf += sent;
  90. length -= sent;
  91. }
  92. return 0;
  93. }
  94. static int WINAPIV send_str (SOCKET s, ...)
  95. {
  96. va_list ap;
  97. char *p;
  98. int ret;
  99. size_t len;
  100. va_start (ap, s);
  101. p = vstrmake (&len, ap);
  102. va_end (ap);
  103. if (!p) return 1;
  104. ret = send_buf (s, p, len);
  105. heap_free (p);
  106. return ret;
  107. }
  108. static int
  109. send_file_direct (const char * url, const char *name)
  110. {
  111. SOCKET s;
  112. HANDLE file;
  113. #define BUFLEN 8192
  114. char buffer[BUFLEN+1];
  115. DWORD bytes_read, filesize;
  116. size_t total, count;
  117. char *str;
  118. int ret;
  119. /* RFC 2616 */
  120. static const char head[] = "POST " URL_PATH " HTTP/1.0\r\n"
  121. "Host: " SERVER_NAME "\r\n"
  122. "User-Agent: " USER_AGENT "\r\n"
  123. CONTENT_HEADERS
  124. "\r\n";
  125. if (url) {
  126. report (R_WARNING, "Can't submit to an alternative URL");
  127. return 0;
  128. }
  129. s = open_http (SERVER_NAME);
  130. if (s == INVALID_SOCKET) return 1;
  131. file = CreateFileA( name, GENERIC_READ,
  132. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  133. NULL, OPEN_EXISTING, 0, NULL );
  134. if ((file == INVALID_HANDLE_VALUE) &&
  135. (GetLastError() == ERROR_INVALID_PARAMETER)) {
  136. /* FILE_SHARE_DELETE not supported on win9x */
  137. file = CreateFileA( name, GENERIC_READ,
  138. FILE_SHARE_READ | FILE_SHARE_WRITE,
  139. NULL, OPEN_EXISTING, 0, NULL );
  140. }
  141. if (file == INVALID_HANDLE_VALUE)
  142. {
  143. report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
  144. goto abort1;
  145. }
  146. report (R_STATUS, "Sending header");
  147. filesize = GetFileSize( file, NULL );
  148. str = strmake (&total, body1, name);
  149. ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
  150. send_buf (s, str, total);
  151. heap_free (str);
  152. if (ret) {
  153. report (R_WARNING, "Error sending header: %d, %d",
  154. errno, WSAGetLastError ());
  155. goto abort2;
  156. }
  157. report (R_STATUS, "Sending %u bytes of data", filesize);
  158. report (R_PROGRESS, 2, filesize);
  159. total = 0;
  160. while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
  161. if (aborting) goto abort2;
  162. if (!bytes_read) break;
  163. total += bytes_read;
  164. if (total > filesize) bytes_read -= total - filesize;
  165. if (send_buf (s, buffer, bytes_read)) {
  166. report (R_WARNING, "Error sending body: %d, %d",
  167. errno, WSAGetLastError ());
  168. goto abort2;
  169. }
  170. report (R_DELTA, bytes_read, "Network transfer: In progress");
  171. }
  172. CloseHandle( file );
  173. if (send_buf (s, body2, sizeof body2 - 1)) {
  174. report (R_WARNING, "Error sending trailer: %d, %d",
  175. errno, WSAGetLastError ());
  176. goto abort1;
  177. }
  178. report (R_DELTA, 0, "Network transfer: Done");
  179. total = 0;
  180. while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
  181. if ((signed)bytes_read == SOCKET_ERROR) {
  182. if (errno == EINTR) continue;
  183. report (R_WARNING, "Error receiving reply: %d, %d",
  184. errno, WSAGetLastError ());
  185. goto abort1;
  186. }
  187. total += bytes_read;
  188. if (total == BUFLEN) {
  189. report (R_WARNING, "Buffer overflow");
  190. goto abort1;
  191. }
  192. }
  193. if (close_http (s)) {
  194. report (R_WARNING, "Error closing connection: %d, %d",
  195. errno, WSAGetLastError ());
  196. return 1;
  197. }
  198. str = strmake (&count, "Received %s (%d bytes).\n",
  199. name, filesize);
  200. ret = total < count || memcmp (str, buffer + total - count, count) != 0;
  201. heap_free (str);
  202. if (ret) {
  203. buffer[total] = 0;
  204. str = strstr (buffer, "\r\n\r\n");
  205. if (!str) str = buffer;
  206. else str = str + 4;
  207. report (R_ERROR, "Can't submit logfile '%s'. "
  208. "Server response: %s", name, str);
  209. }
  210. return ret;
  211. abort2:
  212. CloseHandle( file );
  213. abort1:
  214. close_http (s);
  215. return 1;
  216. }
  217. static int
  218. send_file_wininet (const char *url, const char *name)
  219. {
  220. int ret = 0;
  221. HMODULE wininet_mod = NULL;
  222. HINTERNET (WINAPI *pInternetOpen)(LPCSTR agent, DWORD access_type, LPCSTR proxy_name, LPCSTR proxy_bypass, DWORD flags);
  223. HINTERNET (WINAPI *pInternetConnect)(HINTERNET session, LPCSTR server_name, INTERNET_PORT server_port, LPCSTR username, LPCSTR password, DWORD service, DWORD flags, DWORD_PTR *context);
  224. HINTERNET (WINAPI *pHttpOpenRequest)(HINTERNET connection, LPCSTR verb, LPCSTR object_name, LPCSTR version, LPCSTR referer, LPCSTR *accept_types, DWORD flags, DWORD_PTR context);
  225. BOOL (WINAPI *pHttpSendRequestEx)(HINTERNET request, LPINTERNET_BUFFERSA buffers_in, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
  226. BOOL (WINAPI *pInternetWriteFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written);
  227. BOOL (WINAPI *pHttpEndRequest)(HINTERNET request, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
  228. BOOL (WINAPI *pInternetReadFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read);
  229. BOOL (WINAPI *pInternetCloseHandle)(HINTERNET Handle) = NULL;
  230. HANDLE file = INVALID_HANDLE_VALUE;
  231. DWORD filesize, bytes_read, bytes_written;
  232. size_t total, count;
  233. char *str = NULL;
  234. HINTERNET session = NULL;
  235. HINTERNET connection = NULL;
  236. HINTERNET request = NULL;
  237. INTERNET_BUFFERSA buffers_in = { 0 };
  238. char buffer[BUFLEN+1];
  239. URL_COMPONENTSA uc = { 0 };
  240. static const char extra_headers[] =
  241. CONTENT_HEADERS;
  242. wininet_mod = LoadLibraryA("wininet.dll");
  243. if (wininet_mod == NULL)
  244. goto done;
  245. pInternetOpen = (void *)GetProcAddress(wininet_mod, "InternetOpenA");
  246. pInternetConnect = (void *)GetProcAddress(wininet_mod, "InternetConnectA");
  247. pHttpOpenRequest = (void *)GetProcAddress(wininet_mod, "HttpOpenRequestA");
  248. pHttpSendRequestEx = (void *)GetProcAddress(wininet_mod, "HttpSendRequestExA");
  249. pInternetWriteFile = (void *)GetProcAddress(wininet_mod, "InternetWriteFile");
  250. pHttpEndRequest = (void *)GetProcAddress(wininet_mod, "HttpEndRequestA");
  251. pInternetReadFile = (void *)GetProcAddress(wininet_mod, "InternetReadFile");
  252. pInternetCloseHandle = (void *)GetProcAddress(wininet_mod, "InternetCloseHandle");
  253. if (pInternetOpen == NULL || pInternetConnect == NULL || pHttpOpenRequest == NULL || pHttpSendRequestEx == NULL || pHttpEndRequest == NULL ||
  254. pInternetWriteFile == NULL || pInternetReadFile == NULL || pInternetCloseHandle == NULL) {
  255. goto done;
  256. }
  257. if (url) {
  258. BOOL (WINAPI *pInternetCrackUrlA)(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp);
  259. pInternetCrackUrlA = (void *)GetProcAddress(wininet_mod, "InternetCrackUrlA");
  260. if (pInternetCrackUrlA == NULL)
  261. goto done;
  262. uc.dwStructSize = sizeof(uc);
  263. uc.dwSchemeLength = uc.dwHostNameLength = uc.dwUserNameLength =
  264. uc.dwPasswordLength = uc.dwUrlPathLength = uc.dwExtraInfoLength =
  265. strlen(url) + 1;
  266. uc.lpszScheme = heap_alloc (uc.dwSchemeLength);
  267. uc.lpszHostName = heap_alloc (uc.dwHostNameLength);
  268. uc.lpszUserName = heap_alloc (uc.dwUserNameLength);
  269. uc.lpszPassword = heap_alloc (uc.dwPasswordLength);
  270. uc.lpszUrlPath = heap_alloc (uc.dwUrlPathLength);
  271. uc.lpszExtraInfo = heap_alloc (uc.dwExtraInfoLength);
  272. if (!pInternetCrackUrlA(url, 0, 0, &uc)) {
  273. report (R_WARNING, "Can't parse submit URL '%s'", url);
  274. goto done;
  275. }
  276. if ((uc.nScheme != INTERNET_SCHEME_HTTP && uc.nScheme != INTERNET_SCHEME_HTTPS) || *uc.lpszExtraInfo) {
  277. report (R_WARNING, "Can't submit report to scheme %s or extra info '%s'", uc.lpszScheme, uc.lpszExtraInfo);
  278. goto done;
  279. }
  280. } else {
  281. uc.nScheme = INTERNET_SCHEME_HTTP;
  282. uc.lpszHostName = heap_strdup (SERVER_NAME);
  283. uc.nPort = INTERNET_DEFAULT_HTTP_PORT;
  284. uc.lpszUserName = heap_strdup ("");
  285. uc.lpszPassword = heap_strdup ("");
  286. uc.lpszUrlPath = heap_strdup (URL_PATH);
  287. }
  288. ret = 1;
  289. file = CreateFileA( name, GENERIC_READ,
  290. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  291. NULL, OPEN_EXISTING, 0, NULL );
  292. if ((file == INVALID_HANDLE_VALUE) &&
  293. (GetLastError() == ERROR_INVALID_PARAMETER)) {
  294. /* FILE_SHARE_DELETE not supported on win9x */
  295. file = CreateFileA( name, GENERIC_READ,
  296. FILE_SHARE_READ | FILE_SHARE_WRITE,
  297. NULL, OPEN_EXISTING, 0, NULL );
  298. }
  299. if (file == INVALID_HANDLE_VALUE) {
  300. report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
  301. goto done;
  302. }
  303. report (R_STATUS, "Opening %s connection to %s:%d",
  304. (uc.nScheme == INTERNET_SCHEME_HTTP ? "http" : "https"),
  305. uc.lpszHostName, uc.nPort);
  306. session = pInternetOpen (USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  307. if (session == NULL) {
  308. report (R_WARNING, "Unable to open connection, error %u", GetLastError());
  309. goto done;
  310. }
  311. connection = pInternetConnect (session, uc.lpszHostName, uc.nPort, uc.lpszUserName, uc.lpszPassword, INTERNET_SERVICE_HTTP, 0, 0);
  312. if (connection == NULL) {
  313. report (R_WARNING, "Unable to connect, error %u", GetLastError());
  314. goto done;
  315. }
  316. request = pHttpOpenRequest (connection, "POST", uc.lpszUrlPath, NULL, NULL, NULL,
  317. INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
  318. INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD | (uc.nScheme == INTERNET_SCHEME_HTTPS ? INTERNET_FLAG_SECURE : 0), 0);
  319. if (request == NULL) {
  320. report (R_WARNING, "Unable to open request, error %u", GetLastError());
  321. goto done;
  322. }
  323. report (R_STATUS, "Sending request");
  324. filesize = GetFileSize( file, NULL );
  325. str = strmake (&total, body1, name);
  326. memset(&buffers_in, 0, sizeof(INTERNET_BUFFERSA));
  327. buffers_in.dwStructSize = sizeof(INTERNET_BUFFERSA);
  328. buffers_in.dwBufferTotal = filesize + total + sizeof body2 - 1;
  329. buffers_in.lpcszHeader = strmake (&count, extra_headers, buffers_in.dwBufferTotal);
  330. buffers_in.dwHeadersLength = count;
  331. if (! pHttpSendRequestEx(request, &buffers_in, NULL, 0, 0)) {
  332. report (R_WARNING, "Unable to send request, error %u", GetLastError());
  333. goto done;
  334. }
  335. if (! pInternetWriteFile(request, str, total, &bytes_written) || bytes_written != total) {
  336. report (R_WARNING, "Unable to write body data, error %u", GetLastError());
  337. goto done;
  338. }
  339. report (R_STATUS, "Sending %u bytes of data", filesize);
  340. report (R_PROGRESS, 2, filesize);
  341. total = 0;
  342. while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
  343. if (aborting) goto done;
  344. if (!bytes_read) break;
  345. total += bytes_read;
  346. if (total > filesize) bytes_read -= total - filesize;
  347. if (! pInternetWriteFile (request, buffer, bytes_read, &bytes_written) || bytes_written != bytes_read) {
  348. report (R_WARNING, "Error sending body: %u", GetLastError ());
  349. goto done;
  350. }
  351. report (R_DELTA, bytes_read, "Network transfer: In progress");
  352. }
  353. if (! pInternetWriteFile(request, body2, sizeof body2 - 1, &bytes_written) || bytes_written != sizeof body2 - 1) {
  354. report (R_WARNING, "Unable to write final body data, error %u", GetLastError());
  355. goto done;
  356. }
  357. if (! pHttpEndRequest(request, NULL, 0, 0)) {
  358. report (R_WARNING, "Unable to end request, error %u", GetLastError());
  359. goto done;
  360. }
  361. report (R_DELTA, 0, "Network transfer: Done");
  362. total = 0;
  363. do
  364. {
  365. if (! pInternetReadFile(request, buffer+total, BUFLEN-total, &bytes_read)) {
  366. report (R_WARNING, "Error receiving reply: %u", GetLastError ());
  367. goto done;
  368. }
  369. total += bytes_read;
  370. if (total == BUFLEN) {
  371. report (R_WARNING, "Buffer overflow");
  372. goto done;
  373. }
  374. }
  375. while (bytes_read != 0);
  376. heap_free (str);
  377. str = strmake (&count, "Received %s (%d bytes).\n",
  378. name, filesize);
  379. if (total < count || memcmp (str, buffer + total - count, count) != 0) {
  380. buffer[total] = 0;
  381. report (R_ERROR, "Can't submit logfile '%s'. "
  382. "Server response: %s", name, buffer);
  383. }
  384. done:
  385. heap_free (uc.lpszScheme);
  386. heap_free (uc.lpszHostName);
  387. heap_free (uc.lpszUserName);
  388. heap_free (uc.lpszPassword);
  389. heap_free (uc.lpszUrlPath);
  390. heap_free (uc.lpszExtraInfo);
  391. heap_free((void *)buffers_in.lpcszHeader);
  392. heap_free(str);
  393. if (pInternetCloseHandle != NULL && request != NULL)
  394. pInternetCloseHandle (request);
  395. if (pInternetCloseHandle != NULL && connection != NULL)
  396. pInternetCloseHandle (connection);
  397. if (pInternetCloseHandle != NULL && session != NULL)
  398. pInternetCloseHandle (session);
  399. if (file != INVALID_HANDLE_VALUE)
  400. CloseHandle (file);
  401. if (wininet_mod != NULL)
  402. FreeLibrary (wininet_mod);
  403. return ret;
  404. }
  405. int
  406. send_file (const char *url, const char *name)
  407. {
  408. return send_file_wininet( url, name ) || send_file_direct( url, name );
  409. }