PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ATF2/control-software/epics-3.14.8/extensions/src/ChannelArchiver/ThirdParty/w3c-libwww-5.4.0/Library/src/HTReader.c

http://atf2flightsim.googlecode.com/
C | 320 lines | 235 code | 20 blank | 65 comment | 71 complexity | 34954bea891cfce80a502a806a2a5026 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, IPL-1.0, BSD-3-Clause
  1. /* HTReader.c
  2. ** READ STREAM FROM THE NETWORK USING TCP
  3. **
  4. ** (c) COPYRIGHT MIT 1995.
  5. ** Please first read the full copyright statement in the file COPYRIGH.
  6. ** @(#) $Id: HTReader.c,v 1.1.1.1 2009/03/14 06:44:10 whitegr Exp $
  7. **
  8. ** HISTORY:
  9. ** 6 June 95 HFN Written
  10. */
  11. /* Library Include files */
  12. #include "wwwsys.h"
  13. #include "WWWUtil.h"
  14. #include "WWWCore.h"
  15. #include "HTNetMan.h"
  16. #include "HTReader.h" /* Implemented here */
  17. struct _HTStream {
  18. const HTStreamClass * isa;
  19. /* ... */
  20. };
  21. struct _HTInputStream {
  22. const HTInputStreamClass * isa;
  23. HTChannel * ch;
  24. HTHost * host;
  25. char * write; /* Last byte written */
  26. char * read; /* Last byte read */
  27. int b_read;
  28. char data [INPUT_BUFFER_SIZE]; /* buffer */
  29. };
  30. /* ------------------------------------------------------------------------- */
  31. PRIVATE int HTReader_flush (HTInputStream * me)
  32. {
  33. HTNet * net = HTHost_getReadNet(me->host);
  34. return net && net->readStream ? (*net->readStream->isa->flush)(net->readStream) : HT_OK;
  35. }
  36. PRIVATE int HTReader_free (HTInputStream * me)
  37. {
  38. HTNet * net = HTHost_getReadNet(me->host);
  39. if (net && net->readStream) {
  40. int status = (*net->readStream->isa->_free)(net->readStream);
  41. if (status == HT_OK) net->readStream = NULL;
  42. return status;
  43. }
  44. return HT_OK;
  45. }
  46. PRIVATE int HTReader_abort (HTInputStream * me, HTList * e)
  47. {
  48. HTNet * net = HTHost_getReadNet(me->host);
  49. if (net && net->readStream) {
  50. int status = (*net->readStream->isa->abort)(net->readStream, NULL);
  51. if (status != HT_IGNORE) net->readStream = NULL;
  52. }
  53. return HT_ERROR;
  54. }
  55. #ifdef FIND_SIGNATURES
  56. /* Push data from a socket down a stream
  57. ** -------------------------------------
  58. **
  59. ** This routine is responsible for creating and PRESENTING any
  60. ** graphic (or other) objects described by the file. As this function
  61. ** max reads a chunk of data on size INPUT_BUFFER_SIZE, it can be used
  62. ** with both blocking or non-blocking sockets. It will always return to
  63. ** the event loop, however if we are using blocking I/O then we get a full
  64. ** buffer read, otherwise we get what's available.
  65. **
  66. ** Returns HT_LOADED if finished reading
  67. ** HT_OK if OK, but more to read
  68. ** HT_ERROR if error,
  69. ** HT_WOULD_BLOCK if read or write would block
  70. ** HT_PAUSE if stream is paused
  71. */
  72. PRIVATE char * strnstr(char * haystack, int *pLen, char * needle)
  73. {
  74. int found = 0;
  75. int need = strlen(needle);
  76. int i, start;
  77. for (start = i = 0; i < *pLen; i++)
  78. if (haystack[i] == needle[found]) {
  79. if (++found == need) {
  80. i -= need - 1; /* beginning of string */
  81. *pLen -= i;
  82. return haystack+i;
  83. }
  84. } else {
  85. found = 0;
  86. }
  87. *pLen = 0;
  88. return NULL;
  89. }
  90. #endif /* FIND_SIGNATURES */
  91. PRIVATE int HTReader_read (HTInputStream * me)
  92. {
  93. HTHost * host = me->host;
  94. SOCKET soc = HTChannel_socket(me->ch);
  95. HTNet * net = HTHost_getReadNet(host);
  96. HTRequest * request = HTNet_request(net);
  97. int status;
  98. if (!net->readStream) {
  99. HTTRACE(STREAM_TRACE, "Read Socket. No read stream for net object %p\n" _ net);
  100. return HT_ERROR;
  101. }
  102. /* Read from socket if we got rid of all the data previously read */
  103. do {
  104. /* don't read if we have to push unwritten data from last call */
  105. if (me->write >= me->read) {
  106. if ((me->b_read = NETREAD(soc, me->data, INPUT_BUFFER_SIZE)) < 0) {
  107. #ifdef EAGAIN
  108. if (socerrno==EAGAIN || socerrno==EWOULDBLOCK) /* POSIX */
  109. #else
  110. if (socerrno==EWOULDBLOCK) /* BSD */
  111. #endif
  112. {
  113. HTTRACE(STREAM_TRACE, "Read Socket. WOULD BLOCK fd %d\n" _ soc);
  114. HTHost_register(host, net, HTEvent_READ);
  115. return HT_WOULD_BLOCK;
  116. #ifdef __svr4__
  117. /*
  118. ** In Solaris envirnoment, SIGPOLL is used to signal end
  119. ** of buffer for /dev/audio. If your process is also doing
  120. ** a socket read, it will cause an EINTR error. This
  121. ** error will cause the www library request to
  122. ** terminate prematurly.
  123. */
  124. } else if (socerrno == EINTR) {
  125. continue;
  126. #endif /* __svr4__ */
  127. #ifdef EPIPE
  128. } else if (socerrno == EPIPE) {
  129. HTTRACE(STREAM_TRACE, "Read Socket. got EPIPE\n" _ soc);
  130. goto socketClosed;
  131. #endif /* EPIPE */
  132. #ifdef ECONNRESET
  133. } else if (socerrno == ECONNRESET) {
  134. HTTRACE(STREAM_TRACE, "Read Socket. got ECONNRESET\n" _ soc);
  135. goto socketClosed;
  136. #endif /* ECONNRESET */
  137. #ifdef _WINSOCKAPI_ /* windows */
  138. /*
  139. ** JK: added new tests here, based on the following text:
  140. ** Under BSD Unixes, if the remote peer closes its
  141. ** connection and your program is blocking on recv(), you
  142. ** will get a 0 back from recv(). Winsock behaves the same
  143. ** way, except that it can also return -1, with
  144. ** WSAGetLastError() returning WSAECONNRESET,
  145. ** WSAECONNABORTED or WSAESHUTDOWN, to signal the
  146. ** detectable flavors of abnormal disconnections.
  147. ** (from the Winsock Programmer's FAQ, Warren Young)
  148. */
  149. } else if (socerrno == ECONNABORTED) {
  150. HTTRACE(STREAM_TRACE, "Read Socket. got ECONNABORTED\n" _ soc);
  151. goto socketClosed;
  152. } else if (socerrno == ESHUTDOWN) {
  153. HTTRACE(STREAM_TRACE, "Read Socket. got ESHUTDOWN\n" _ soc);
  154. goto socketClosed;
  155. #endif /* _WINSOCKAPI */
  156. } else { /* We have a real error */
  157. if (request)
  158. HTRequest_addSystemError(request, ERR_FATAL, socerrno,
  159. NO, "NETREAD");
  160. return HT_ERROR;
  161. }
  162. } else if (!me->b_read) {
  163. socketClosed:
  164. HTTRACE(STREAM_TRACE, "Read Socket. FIN received on socket %d\n" _ soc);
  165. HTHost_unregister(host, net, HTEvent_READ);
  166. HTHost_register(host, net, HTEvent_CLOSE);
  167. return HT_CLOSED;
  168. }
  169. /* Remember how much we have read from the input socket */
  170. HTTRACEDATA(me->data, me->b_read, "Reading from socket %d" _ soc);
  171. me->write = me->data;
  172. me->read = me->data + me->b_read;
  173. #ifdef FIND_SIGNATURES
  174. {
  175. char * ptr = me->data;
  176. int len = me->b_read;
  177. while ((ptr = strnstr(ptr, &len, "HTTP/1.1 200 OK")) != NULL) {
  178. HTTRACE(STREAM_TRACE, "Read Socket. Signature found at 0x%x of 0x%x.\n" _ ptr - me->data _ me->b_read);
  179. ptr++;
  180. len--;
  181. }
  182. }
  183. #endif /* FIND_SIGNATURES */
  184. #ifdef NOT_ASCII
  185. {
  186. char *p = me->data;
  187. while (p < me->read) {
  188. *p = FROMASCII(*p);
  189. p++;
  190. }
  191. }
  192. #endif /* NOT_ASCII */
  193. HTTRACE(STREAM_TRACE, "Read Socket. %d bytes read from socket %d\n" _
  194. me->b_read _ soc);
  195. if (request) {
  196. HTAlertCallback * cbf = HTAlert_find(HT_PROG_READ);
  197. if (HTNet_rawBytesCount(net))
  198. HTNet_addBytesRead(net, me->b_read);
  199. if (cbf) {
  200. int tr = HTNet_bytesRead(net);
  201. (*cbf)(request, HT_PROG_READ, HT_MSG_NULL, NULL, &tr, NULL);
  202. }
  203. }
  204. }
  205. /* Now push the data down the stream */
  206. if ((status = (*net->readStream->isa->put_block)
  207. (net->readStream, me->write, me->b_read)) != HT_OK) {
  208. if (status == HT_WOULD_BLOCK) {
  209. HTTRACE(STREAM_TRACE, "Read Socket. Target WOULD BLOCK\n");
  210. HTHost_unregister(host, net, HTEvent_READ);
  211. return HT_WOULD_BLOCK;
  212. } else if (status == HT_PAUSE) {
  213. HTTRACE(STREAM_TRACE, "Read Socket. Target PAUSED\n");
  214. HTHost_unregister(host, net, HTEvent_READ);
  215. return HT_PAUSE;
  216. /* CONTINUE code or stream code means data was consumed */
  217. } else if (status == HT_CONTINUE || status > 0) {
  218. if (status == HT_CONTINUE) {
  219. HTTRACE(STREAM_TRACE, "Read Socket. CONTINUE\n");
  220. } else
  221. HTTRACE(STREAM_TRACE, "Read Socket. Target returns %d\n" _ status);
  222. /* me->write = me->read; */
  223. return status;
  224. } else { /* We have a real error */
  225. HTTRACE(STREAM_TRACE, "Read Socket. Target ERROR %d\n" _ status);
  226. return status;
  227. }
  228. }
  229. me->write = me->read;
  230. {
  231. int remaining = HTHost_remainingRead(host);
  232. if (remaining > 0) {
  233. HTTRACE(STREAM_TRACE, "Read Socket. DIDN'T CONSUME %d BYTES: `%s\'\n" _
  234. remaining _ me->read);
  235. HTHost_setConsumed(host, remaining);
  236. }
  237. }
  238. } while (net->preemptive);
  239. HTHost_register(host, net, HTEvent_READ);
  240. return HT_WOULD_BLOCK;
  241. }
  242. /*
  243. ** The difference between the close and the free method is that we don't
  244. ** close the connection in the free method - we only call the free method
  245. ** of the target stream. That way, we can keep the input stream as long
  246. ** as the channel itself.
  247. */
  248. PRIVATE int HTReader_close (HTInputStream * me)
  249. {
  250. int status = HT_OK;
  251. HTNet * net = HTHost_getReadNet(me->host);
  252. if (net && net->readStream) {
  253. if ((status = (*net->readStream->isa->_free)(net->readStream))==HT_WOULD_BLOCK)
  254. return HT_WOULD_BLOCK;
  255. net->readStream = NULL;
  256. }
  257. HTTRACE(STREAM_TRACE, "Socket read. FREEING....\n");
  258. HT_FREE(me);
  259. return status;
  260. }
  261. PUBLIC int HTReader_consumed (HTInputStream * me, size_t bytes)
  262. {
  263. me->write += bytes;
  264. me->b_read -= bytes;
  265. HTHost_setRemainingRead(me->host, me->b_read);
  266. return HT_OK;
  267. }
  268. PRIVATE const HTInputStreamClass HTReader =
  269. {
  270. "SocketReader",
  271. HTReader_flush,
  272. HTReader_free,
  273. HTReader_abort,
  274. HTReader_read,
  275. HTReader_close,
  276. HTReader_consumed
  277. };
  278. /*
  279. ** Create a new input read stream. Before we actually create it we check
  280. ** to see whether we already have an input stream for this channel and if
  281. ** so we just return that. This means that we can reuse input streams
  282. ** in persistent connections, for example.
  283. */
  284. PUBLIC HTInputStream * HTReader_new (HTHost * host, HTChannel * ch,
  285. void * param, int mode)
  286. {
  287. if (host && ch) {
  288. HTInputStream * me = HTChannel_input(ch);
  289. if (me == NULL) {
  290. if ((me=(HTInputStream *) HT_CALLOC(1, sizeof(HTInputStream))) == NULL)
  291. HT_OUTOFMEM("HTReader_new");
  292. me->isa = &HTReader;
  293. me->ch = ch;
  294. me->host = host;
  295. HTTRACE(STREAM_TRACE, "Reader...... Created reader stream %p\n" _ me);
  296. }
  297. return me;
  298. }
  299. return NULL;
  300. }