windows_net.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <iron_system.h>
  2. #include <iron_net.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <Windows.h>
  6. #include <winhttp.h>
  7. static const wchar_t *convert(int method) {
  8. switch (method) {
  9. case IRON_HTTP_GET:
  10. default:
  11. return L"GET";
  12. case IRON_HTTP_POST:
  13. return L"POST";
  14. case IRON_HTTP_PUT:
  15. return L"PUT";
  16. case IRON_HTTP_DELETE:
  17. return L"DELETE";
  18. }
  19. }
  20. static char *returnData = NULL;
  21. static int returnDataSize = 0;
  22. void iron_http_request(const char *url, const char *path, const char *data, int port, bool secure, int method, const char *header,
  23. iron_http_callback_t callback, void *callbackdata) {
  24. // based on https://docs.microsoft.com/en-us/windows/desktop/winhttp/winhttp-sessions-overview
  25. HINTERNET hSession = WinHttpOpen(L"WinHTTP via Iron/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
  26. HINTERNET hConnect = NULL;
  27. if (hSession) {
  28. wchar_t wurl[4096];
  29. MultiByteToWideChar(CP_UTF8, 0, url, -1, wurl, 4096);
  30. hConnect = WinHttpConnect(hSession, wurl, port, 0);
  31. }
  32. HINTERNET hRequest = NULL;
  33. if (hConnect) {
  34. wchar_t wpath[4096];
  35. MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, 4096);
  36. hRequest =
  37. WinHttpOpenRequest(hConnect, convert(method), wpath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, secure ? WINHTTP_FLAG_SECURE : 0);
  38. }
  39. BOOL bResults = FALSE;
  40. if (hRequest) {
  41. wchar_t wheader[4096];
  42. if (header) {
  43. MultiByteToWideChar(CP_UTF8, 0, header, -1, wheader, 4096);
  44. }
  45. DWORD optionalLength = (data != 0 && strlen(data) > 0) ? (DWORD)strlen(data) : 0;
  46. bResults = WinHttpSendRequest(hRequest, header == 0 ? WINHTTP_NO_ADDITIONAL_HEADERS : wheader, header == 0 ? 0 : -1L,
  47. data == 0 ? WINHTTP_NO_REQUEST_DATA : (LPVOID)data, optionalLength, optionalLength, 0);
  48. }
  49. if (bResults)
  50. bResults = WinHttpReceiveResponse(hRequest, NULL);
  51. int returnDataIndex = 0;
  52. if (bResults) {
  53. DWORD dwSize;
  54. do {
  55. dwSize = 0;
  56. if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
  57. iron_error("Error %d in WinHttpQueryDataAvailable.\n", GetLastError());
  58. }
  59. if ((int)dwSize + 1 > returnDataSize - returnDataIndex) {
  60. int newReturnDataSize = (returnDataIndex + dwSize + 1) * 2;
  61. char *newReturnData = (char *)malloc(newReturnDataSize);
  62. if (newReturnData == 0) {
  63. iron_error("Out of memory\n");
  64. }
  65. memcpy(newReturnData, returnData, returnDataSize);
  66. returnDataSize = newReturnDataSize;
  67. returnData = newReturnData;
  68. }
  69. DWORD dwDownloaded = 0;
  70. if (!WinHttpReadData(hRequest, (LPVOID)(&returnData[returnDataIndex]), dwSize, &dwDownloaded)) {
  71. iron_error("Error %d in WinHttpReadData.\n", GetLastError());
  72. }
  73. returnDataIndex += dwSize;
  74. } while (dwSize > 0);
  75. }
  76. else {
  77. callback(1, 404, NULL, callbackdata);
  78. return;
  79. }
  80. returnData[returnDataIndex] = 0;
  81. if (!bResults) {
  82. iron_error("Error %d has occurred.\n", GetLastError());
  83. }
  84. if (hRequest) {
  85. WinHttpCloseHandle(hRequest);
  86. }
  87. if (hConnect) {
  88. WinHttpCloseHandle(hConnect);
  89. }
  90. if (hSession) {
  91. WinHttpCloseHandle(hSession);
  92. }
  93. callback(0, 200, returnData, callbackdata);
  94. }