1
0

WGET.C 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // Wget
  2. // Tool to do a HTTP GET request
  3. #define word char
  4. #include "LIB/MATH.C"
  5. #include "LIB/STDLIB.C"
  6. #include "LIB/SYS.C"
  7. #include "LIB/FS.C"
  8. #include "LIB/WIZ5500.C"
  9. #define HEAP_LOCATION 0x500000
  10. // Which socket to use
  11. #define SOCKET_CLIENT 0
  12. #define CONNECT_MAX_RETRIES 1000
  13. // Initializes w5500 in client TCP mode, with a static ip (last digit as arg)
  14. // Port is also given as arg
  15. void initClient(word ip, word port)
  16. {
  17. word ip_addr[4] = {192, 168, 0, 213};
  18. word gateway_addr[4] = {192, 168, 0, 1};
  19. word mac_addr[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0x24, 0x64};
  20. word sub_mask[4] = {255, 255, 255, 0};
  21. wiz_Init(ip_addr, gateway_addr, mac_addr, sub_mask);
  22. // Open socket in TCP Client mode at port
  23. wizInitSocketTCPClient(SOCKET_CLIENT, port);
  24. }
  25. // Connect to target at given ip and port
  26. // Return 1 on success, 0 on failure
  27. word connectToTarget(char* ipTarget, word portTarget)
  28. {
  29. wizWrite(WIZNET_SnDIPR, WIZNET_WRITE_SnR + (SOCKET_CLIENT << 5), ipTarget, 4);
  30. wizSetSockReg16(SOCKET_CLIENT, WIZNET_SnDPORT, portTarget);
  31. wizCmd(SOCKET_CLIENT, WIZNET_CR_CONNECT);
  32. word sxStatus = wizGetSockReg8(SOCKET_CLIENT, WIZNET_SnSR);
  33. word retries = 0;
  34. while (sxStatus != WIZNET_SOCK_ESTABLISHED && retries < CONNECT_MAX_RETRIES)
  35. {
  36. //BDOS_PrintHexConsole(sxStatus);
  37. if (sxStatus == WIZNET_SOCK_CLOSED)
  38. {
  39. return 0;
  40. }
  41. sxStatus = wizGetSockReg8(SOCKET_CLIENT, WIZNET_SnSR);
  42. if (sxStatus != WIZNET_SOCK_ESTABLISHED)
  43. {
  44. retries++;
  45. delay(1);
  46. }
  47. }
  48. return 1;
  49. }
  50. // Send request (headers should use \r\n according to the HTTP specs)
  51. void sendRequest()
  52. {
  53. char* rTxt = (char*) HEAP_LOCATION; // we use heap for the request text
  54. word rLen = 0;
  55. word catLen = 0;
  56. catLen = strcpy(rTxt + rLen, "GET /");
  57. rLen += catLen;
  58. char targetURL[64];
  59. BDOS_GetArgN(2, targetURL);
  60. catLen = strcpy(rTxt + rLen, targetURL);
  61. rLen += catLen;
  62. catLen = strcpy(rTxt + rLen, " HTTP/1.1\r\nUser-Agent: WGET(FPGC)\r\nAccept: */*\r\nHost: ");
  63. rLen += catLen;
  64. char ipstr[24];
  65. BDOS_GetArgN(1, ipstr);
  66. catLen = strcpy(rTxt + rLen, ipstr);
  67. rLen += catLen;
  68. catLen = strcpy(rTxt + rLen, "\r\n\r\n");
  69. rLen += catLen;
  70. wizWriteDataFromMemory(SOCKET_CLIENT, rTxt, rLen);
  71. BDOS_PrintConsole("Request sent\n");
  72. }
  73. // Init output file for download
  74. // Returns 1 on success, else 0
  75. word initOutputFile(char* path, char* fname, char* rbuf, word rsize)
  76. {
  77. // sanity check current path
  78. if (FS_sendFullPath(path) == FS_ANSW_USB_INT_SUCCESS)
  79. {
  80. word retval = FS_open();
  81. // check that we can open the path
  82. if (retval == FS_ANSW_USB_INT_SUCCESS || retval == FS_ANSW_ERR_OPEN_DIR)
  83. {
  84. // check length of filename
  85. if (strlen(fname) <= 12)
  86. {
  87. // uppercase filename
  88. strToUpper(fname);
  89. // send filename
  90. FS_sendSinglePath(fname);
  91. // create the file
  92. if (FS_createFile() == FS_ANSW_USB_INT_SUCCESS)
  93. {
  94. // open the path again
  95. FS_sendFullPath(path);
  96. FS_open();
  97. // send filename again
  98. FS_sendSinglePath(fname);
  99. // open the newly created file
  100. if (FS_open() == FS_ANSW_USB_INT_SUCCESS)
  101. {
  102. // set cursor to start
  103. if (FS_setCursor(0) == FS_ANSW_USB_INT_SUCCESS)
  104. {
  105. if (FS_writeFile(rbuf, rsize) != FS_ANSW_USB_INT_SUCCESS)
  106. {
  107. FS_close();
  108. BDOS_PrintConsole("Error while writing to file\n");
  109. return 0;
  110. }
  111. }
  112. else
  113. {
  114. FS_close();
  115. BDOS_PrintConsole("Cursor error\n");
  116. return 0;
  117. }
  118. }
  119. }
  120. else
  121. {
  122. BDOS_PrintConsole("Error while creating output file\n");
  123. return 0;
  124. }
  125. }
  126. else
  127. {
  128. BDOS_PrintConsole("Filename too long\n");
  129. return 0;
  130. }
  131. }
  132. else
  133. {
  134. BDOS_PrintConsole("Could not open path\n");
  135. return 0;
  136. }
  137. }
  138. else
  139. {
  140. BDOS_PrintConsole("Error sending path\n");
  141. return 0;
  142. }
  143. return 1;
  144. }
  145. // Parses HTTP header, returns location of the first data byte
  146. word getDataStart(char* rbuf, word rsize)
  147. {
  148. word dataStart = 0;
  149. word i = 0;
  150. while (i < rsize - 4 && dataStart == 0)
  151. {
  152. //BDOS_PrintcConsole(rbuf[i]);
  153. if (rbuf[i] == '\r' && rbuf[i+1] == '\n' && rbuf[i+2] == '\r' && rbuf[i+3] == '\n')
  154. {
  155. dataStart = i+4;
  156. }
  157. else if (rbuf[i] == '\n' && rbuf[i+1] == '\n')
  158. {
  159. dataStart = i+2;
  160. }
  161. i++;
  162. }
  163. return dataStart;
  164. }
  165. // Parses HTTP header, returns status code
  166. word getStatusCode(char* rbuf, word rsize)
  167. {
  168. word i = 0;
  169. // look for first space, then 3 digits
  170. while (i < rsize - 4)
  171. {
  172. if (rbuf[i] == ' ')
  173. {
  174. char statusStr[4];
  175. statusStr[0] = rbuf[i+1];
  176. statusStr[1] = rbuf[i+2];
  177. statusStr[2] = rbuf[i+3];
  178. statusStr[3] = 0;
  179. return strToInt(statusStr);
  180. }
  181. i++;
  182. }
  183. return 0;
  184. }
  185. // Parses HTTP header, returns content length header field
  186. word getContentLength(char* rbuf, word rsize)
  187. {
  188. word i = 0;
  189. word strBufIndex = 0;
  190. // look for Content-Length field
  191. while (i < rsize - 4)
  192. {
  193. if (memcmp(&rbuf[i], "Content-Length: ", 16))
  194. {
  195. i = i + 16;
  196. // when found, add all following characters to buffer until newline
  197. char contentLengthStrBuf[32];
  198. contentLengthStrBuf[0] = 0;
  199. while (rbuf[i] != '\r' && rbuf[i] != '\n')
  200. {
  201. contentLengthStrBuf[strBufIndex] = rbuf[i];
  202. i++;
  203. strBufIndex++;
  204. }
  205. contentLengthStrBuf[strBufIndex] = 0; // terminate buffer
  206. // finally, convert to int and return
  207. return strToInt(contentLengthStrBuf);
  208. }
  209. i++;
  210. }
  211. return 0;
  212. }
  213. word getPercentageDone(word remaining, word full)
  214. {
  215. word x = remaining * 100;
  216. return 100 - MATH_divU(x, full);
  217. }
  218. // Receive response while connected
  219. // Write to file and terminal
  220. void receiveResponse(char* path, char* fname)
  221. {
  222. word firstResponse = 1;
  223. word contentLengthOrig[1]; // its an array now, because of a compiler bug in DeprCcompiler (ShivyC)
  224. contentLengthOrig[0] = 0;
  225. word contentLength = 0;
  226. word currentProgress = 0;
  227. BDOS_PrintConsole("Downloading response (any key to stop)\n");
  228. while (wizGetSockReg8(SOCKET_CLIENT, WIZNET_SnSR) == WIZNET_SOCK_ESTABLISHED)
  229. {
  230. word rsize = wizGetSockReg16(SOCKET_CLIENT, WIZNET_SnRX_RSR);
  231. if (rsize != 0)
  232. {
  233. char* rbuf = (char*) HEAP_LOCATION; // we use heap for the receive data buffer
  234. wizReadRecvData(SOCKET_CLIENT, rbuf, rsize);
  235. if (firstResponse)
  236. {
  237. word dataStart = getDataStart(rbuf, rsize);
  238. contentLength = getContentLength(rbuf, rsize);
  239. contentLengthOrig[0] = contentLength;
  240. BDOS_PrintConsole("Status code: ");
  241. BDOS_PrintDecConsole(getStatusCode(rbuf, rsize));
  242. BDOS_PrintConsole("\n");
  243. if (!initOutputFile(path, fname, &rbuf[dataStart], rsize - dataStart))
  244. return;
  245. contentLength -= (rsize - dataStart);
  246. firstResponse = 0;
  247. // all data downloaded
  248. if (contentLength == 0)
  249. break;
  250. }
  251. else
  252. {
  253. if (FS_writeFile(rbuf, rsize) != FS_ANSW_USB_INT_SUCCESS)
  254. {
  255. FS_close();
  256. BDOS_PrintConsole("Error while writing to file\n");
  257. return;
  258. }
  259. contentLength -= rsize;
  260. if (getPercentageDone(contentLength, contentLengthOrig[0]) >= currentProgress)
  261. {
  262. BDOS_PrintDecConsole(currentProgress);
  263. BDOS_PrintConsole("%\n");
  264. currentProgress += 10;
  265. }
  266. // all data downloaded
  267. if (contentLength == 0)
  268. break;
  269. }
  270. }
  271. // Allow manual cancel
  272. if (HID_FifoAvailable())
  273. {
  274. HID_FifoRead(); // remove it from the buffer
  275. break;
  276. }
  277. }
  278. wizCmd(SOCKET_CLIENT, WIZNET_CR_DISCON);
  279. BDOS_PrintConsole("Output written to ");
  280. BDOS_PrintConsole(path);
  281. BDOS_PrintConsole("/");
  282. BDOS_PrintConsole(fname);
  283. BDOS_PrintConsole("\n");
  284. FS_close();
  285. }
  286. // convert string to IP by looking at the dots
  287. void strToIP(char* str, char* IP)
  288. {
  289. word i = 0;
  290. word dotsFound = 0;
  291. word digitCounter = 0;
  292. char tmp[16];
  293. while (str[i] != 0)
  294. {
  295. if (str[i] == '.')
  296. {
  297. tmp[digitCounter] = 0;
  298. IP[dotsFound] = strToInt(tmp);
  299. dotsFound += 1;
  300. digitCounter = 0;
  301. }
  302. else
  303. {
  304. tmp[digitCounter] = str[i];
  305. digitCounter++;
  306. }
  307. i++;
  308. }
  309. tmp[digitCounter] = 0;
  310. IP[dotsFound] = strToInt(tmp);
  311. }
  312. int main()
  313. {
  314. // Init
  315. BDOS_PrintConsole("WGET (IP path output [port])\n");
  316. initClient(213, 1024);
  317. // TODO: DNS lookup
  318. // IP
  319. char ipstr[24];
  320. BDOS_GetArgN(1, ipstr);
  321. char ipTarget[4];
  322. strToIP(ipstr, ipTarget);
  323. // Port
  324. word portTarget = 0;
  325. char portstr[16];
  326. BDOS_GetArgN(4, portstr);
  327. if (portstr[0] != 0)
  328. portTarget = strToInt(portstr);
  329. if (portTarget == 0)
  330. portTarget = 80;
  331. // Connect to target
  332. if (!connectToTarget(ipTarget, portTarget))
  333. {
  334. BDOS_PrintConsole("Could not connect to host\n");
  335. return 'q';
  336. }
  337. else
  338. BDOS_PrintConsole("Connected to host\n");
  339. // Send request
  340. sendRequest();
  341. // Receive response
  342. char* path = BDOS_GetPath();
  343. // If no filename is given, use OUT.TXT as default
  344. char fname[16];
  345. BDOS_GetArgN(3, fname);
  346. if (fname[0] == 0)
  347. {
  348. strcpy(fname, "OUT.TXT");
  349. }
  350. receiveResponse(path, fname);
  351. return 'q';
  352. }
  353. void interrupt()
  354. {
  355. // handle all interrupts
  356. word i = getIntID();
  357. switch(i)
  358. {
  359. case INTID_TIMER1:
  360. timer1Value = 1; // notify ending of timer1
  361. break;
  362. case INTID_TIMER2:
  363. break;
  364. case INTID_UART0:
  365. break;
  366. case INTID_GPU:
  367. break;
  368. case INTID_TIMER3:
  369. break;
  370. case INTID_PS2:
  371. break;
  372. case INTID_UART1:
  373. break;
  374. case INTID_UART2:
  375. break;
  376. }
  377. }