streamio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2012 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Id$
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <stdarg.h>
  40. #include <time.h>
  41. #include <process.h>
  42. #include <pro/uhttp/streamio.h>
  43. static int num_threads;
  44. int StreamInit(void)
  45. {
  46. WSADATA wsa;
  47. if (WSAStartup(MAKEWORD(2,2), &wsa)) {
  48. printf("Failed. Error Code : %d", WSAGetLastError());
  49. return -1;
  50. }
  51. return 0;
  52. }
  53. typedef struct _CLIENT_THREAD_PARAM {
  54. HTTP_STREAM *ctp_stream;
  55. HTTP_CLIENT_HANDLER ctp_handler;
  56. } CLIENT_THREAD_PARAM;
  57. void StreamClientThread(void *param)
  58. {
  59. CLIENT_THREAD_PARAM *ctp = (CLIENT_THREAD_PARAM *) param;
  60. (*ctp->ctp_handler)(ctp->ctp_stream);
  61. closesocket(ctp->ctp_stream->strm_csock);
  62. free(ctp->ctp_stream);
  63. free(ctp);
  64. if (--num_threads == 0) {
  65. _CrtDumpMemoryLeaks();
  66. }
  67. }
  68. int StreamClientAccept(HTTP_CLIENT_HANDLER handler, const char *params)
  69. {
  70. int rc = -1;
  71. SOCKET sock;
  72. SOCKET csock;
  73. struct sockaddr_in addr;
  74. struct sockaddr_in caddr;
  75. unsigned short port = 80;
  76. HTTP_ASSERT(handler != NULL);
  77. if (params) {
  78. port = (unsigned short)atoi(params);
  79. }
  80. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  81. if (sock != INVALID_SOCKET) {
  82. memset((void*)&addr, 0, sizeof(addr));
  83. addr.sin_family = AF_INET;
  84. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  85. addr.sin_port = htons(port);
  86. if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
  87. if (listen(sock, 1) == 0) {
  88. socklen_t len = sizeof(caddr);
  89. for (;;) {
  90. csock = accept(sock, (struct sockaddr*)&caddr, &len);
  91. if (csock != INVALID_SOCKET) {
  92. CLIENT_THREAD_PARAM *ctp;
  93. unsigned int tmo = 1000;
  94. setsockopt(csock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tmo, sizeof(tmo));
  95. ctp = malloc(sizeof(CLIENT_THREAD_PARAM));
  96. ctp->ctp_handler = handler;
  97. ctp->ctp_stream = calloc(1, sizeof(HTTP_STREAM));
  98. ctp->ctp_stream->strm_ssock = sock;
  99. ctp->ctp_stream->strm_csock = csock;
  100. memcpy(&ctp->ctp_stream->strm_saddr, &addr, sizeof(ctp->ctp_stream->strm_saddr));
  101. memcpy(&ctp->ctp_stream->strm_caddr, &caddr, sizeof(ctp->ctp_stream->strm_caddr));
  102. num_threads++;
  103. while (num_threads > 2) {
  104. Sleep(100);
  105. }
  106. _beginthread(StreamClientThread, 0, ctp);
  107. }
  108. }
  109. }
  110. }
  111. closesocket(sock);
  112. }
  113. return rc;
  114. }
  115. int StreamReadUntilChars(HTTP_STREAM *sp, const char *delim, const char *ignore, char *buf, int siz)
  116. {
  117. int rc = 0;
  118. int skip = 0;
  119. char ch;
  120. HTTP_ASSERT(sp != NULL);
  121. /* Do not read more characters than requested. */
  122. while (rc < siz) {
  123. /* Check the current stream buffer. */
  124. if (sp->strm_ipos == sp->strm_ilen) {
  125. /* No more buffered data, re-fill the buffer. */
  126. int got = recv(sp->strm_csock, sp->strm_ibuf, 1460, 0);
  127. if (got <= 0) {
  128. /* Broken connection or timeout. */
  129. if (got < 0) {
  130. rc = -1;
  131. skip = 0;
  132. }
  133. break;
  134. }
  135. sp->strm_ilen = got;
  136. sp->strm_ipos = 0;
  137. }
  138. ch = sp->strm_ibuf[sp->strm_ipos];
  139. sp->strm_ipos++;
  140. if (rc == 0 && ch == ' ') {
  141. /* Skip leading spaces. */
  142. skip++;
  143. } else {
  144. rc++;
  145. if (delim && strchr(delim, ch)) {
  146. /* Delimiter found. */
  147. break;
  148. }
  149. if (buf && (ignore == NULL || strchr(ignore, ch) == NULL)) {
  150. /* Add valid character to application buffer. */
  151. *buf++ = ch;
  152. }
  153. }
  154. }
  155. if (buf) {
  156. *buf = '\0';
  157. }
  158. return rc + skip;
  159. }
  160. int StreamReadUntilString(HTTP_STREAM *sp, const char *delim, char *buf, int siz)
  161. {
  162. int rc = 0;
  163. int n;
  164. int i;
  165. int delen = strlen(delim);
  166. HTTP_ASSERT(sp != NULL);
  167. /* Do not read more characters than requested. */
  168. while (rc < siz) {
  169. /* Check if the delimiter fits in the current stream buffer. */
  170. if (sp->strm_ipos >= sp->strm_ilen - delen) {
  171. int got;
  172. /* Not enough data to fit the delimiter, re-fill the buffer. */
  173. sp->strm_ilen -= sp->strm_ipos;
  174. memcpy(sp->strm_ibuf, sp->strm_ibuf + sp->strm_ipos, sp->strm_ilen);
  175. got = recv(sp->strm_csock, sp->strm_ibuf + sp->strm_ilen, sizeof(sp->strm_ibuf) - sp->strm_ilen, 0);
  176. if (got <= 0) {
  177. /* Broken connection or timeout. */
  178. if (got < 0) {
  179. rc = -1;
  180. }
  181. break;
  182. }
  183. sp->strm_ilen += got;
  184. sp->strm_ipos = 0;
  185. }
  186. for (i = sp->strm_ipos, n = 0; i < sp->strm_ilen && rc + n < siz; i++, n++) {
  187. if (*delim == sp->strm_ibuf[i]) {
  188. if (i + delen >= sp->strm_ilen) {
  189. break;
  190. }
  191. if (memcmp(&sp->strm_ibuf[i], delim, delen) == 0) {
  192. break;
  193. }
  194. }
  195. }
  196. if (n) {
  197. memcpy(buf, sp->strm_ibuf + sp->strm_ipos, n);
  198. buf += n;
  199. rc += n;
  200. sp->strm_ipos += n;
  201. } else {
  202. break;
  203. }
  204. }
  205. return rc;
  206. }
  207. int s_write(const void *buf, size_t size, size_t count, HTTP_STREAM *sp)
  208. {
  209. HTTP_ASSERT(sp != NULL);
  210. HTTP_ASSERT(buf != NULL);
  211. return send(sp->strm_csock, (const char *)buf, size * count, 0);
  212. }
  213. int s_puts(const char *str, HTTP_STREAM *sp)
  214. {
  215. HTTP_ASSERT(sp != NULL);
  216. HTTP_ASSERT(str != NULL);
  217. return send(sp->strm_csock, str, strlen(str), 0);
  218. }
  219. int s_vputs(HTTP_STREAM *sp, ...)
  220. {
  221. int rc = -1;
  222. int len;
  223. char *cp;
  224. char *buf;
  225. va_list ap;
  226. HTTP_ASSERT(sp != NULL);
  227. va_start(ap, sp);
  228. for (len = 0; (cp = va_arg(ap, char *)) != NULL; len += strlen(cp));
  229. va_end(ap);
  230. buf = malloc(len + 1);
  231. if (buf) {
  232. va_start(ap, sp);
  233. for (*buf = '\0'; (cp = va_arg(ap, char *)) != NULL; strcat(buf, cp));
  234. va_end(ap);
  235. rc = send(sp->strm_csock, buf, strlen(buf), 0);
  236. free(buf);
  237. }
  238. return rc;
  239. }
  240. int s_printf(HTTP_STREAM *sp, const char *fmt, ...)
  241. {
  242. int rc = -1;
  243. char *buf;
  244. va_list ap;
  245. HTTP_ASSERT(sp != NULL);
  246. HTTP_ASSERT(fmt != NULL);
  247. buf = malloc(1024);
  248. if (buf) {
  249. va_start(ap, fmt);
  250. rc = vsnprintf(buf, 1023, fmt, ap);
  251. va_end(ap);
  252. rc = send(sp->strm_csock, buf, rc, 0);
  253. free(buf);
  254. }
  255. return rc;
  256. }
  257. int s_flush(HTTP_STREAM *sp)
  258. {
  259. return 0;
  260. }
  261. const char *StreamInfo(HTTP_STREAM *sp, int item)
  262. {
  263. static char *env_value;
  264. char *vp = NULL;
  265. free(env_value);
  266. env_value = NULL;
  267. switch (item) {
  268. case SITEM_REMOTE_ADDR:
  269. env_value = strdup(inet_ntoa(sp->strm_caddr.sin_addr));
  270. break;
  271. case SITEM_REMOTE_PORT:
  272. env_value = malloc(16);
  273. sprintf(env_value, "%u", sp->strm_caddr.sin_port);
  274. break;
  275. case SITEM_SERVER_NAME:
  276. case SITEM_SERVER_ADDR:
  277. env_value = strdup(inet_ntoa(sp->strm_saddr.sin_addr));
  278. break;
  279. case SITEM_SERVER_PORT:
  280. env_value = malloc(16);
  281. sprintf(env_value, "%u", sp->strm_saddr.sin_port);
  282. break;
  283. }
  284. if (env_value == NULL) {
  285. env_value = strdup("");
  286. }
  287. return env_value;
  288. }