2
0

apr_buckets_socket.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr_buckets.h"
  17. static apr_status_t socket_bucket_read(apr_bucket *a, const char **str,
  18. apr_size_t *len, apr_read_type_e block)
  19. {
  20. apr_socket_t *p = a->data;
  21. char *buf;
  22. apr_status_t rv;
  23. apr_interval_time_t timeout;
  24. if (block == APR_NONBLOCK_READ) {
  25. apr_socket_timeout_get(p, &timeout);
  26. apr_socket_timeout_set(p, 0);
  27. }
  28. *str = NULL;
  29. *len = APR_BUCKET_BUFF_SIZE;
  30. buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
  31. rv = apr_socket_recv(p, buf, len);
  32. if (block == APR_NONBLOCK_READ) {
  33. apr_socket_timeout_set(p, timeout);
  34. }
  35. if (rv != APR_SUCCESS && rv != APR_EOF) {
  36. apr_bucket_free(buf);
  37. return rv;
  38. }
  39. /*
  40. * If there's more to read we have to keep the rest of the socket
  41. * for later. XXX: Note that more complicated bucket types that
  42. * refer to data not in memory and must therefore have a read()
  43. * function similar to this one should be wary of copying this
  44. * code because if they have a destroy function they probably
  45. * want to migrate the bucket's subordinate structure from the
  46. * old bucket to a raw new one and adjust it as appropriate,
  47. * rather than destroying the old one and creating a completely
  48. * new bucket.
  49. *
  50. * Even if there is nothing more to read, don't close the socket here
  51. * as we have to use it to send any response :) We could shut it
  52. * down for reading, but there is no benefit to doing so.
  53. */
  54. if (*len > 0) {
  55. apr_bucket_heap *h;
  56. /* Change the current bucket to refer to what we read */
  57. a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
  58. h = a->data;
  59. h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
  60. *str = buf;
  61. APR_BUCKET_INSERT_AFTER(a, apr_bucket_socket_create(p, a->list));
  62. }
  63. else {
  64. apr_bucket_free(buf);
  65. a = apr_bucket_immortal_make(a, "", 0);
  66. *str = a->data;
  67. }
  68. return APR_SUCCESS;
  69. }
  70. APU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b, apr_socket_t *p)
  71. {
  72. /*
  73. * XXX: We rely on a cleanup on some pool or other to actually
  74. * destroy the socket. We should probably explicitly call apr to
  75. * destroy it instead.
  76. *
  77. * Note that typically the socket is allocated from the connection pool
  78. * so it will disappear when the connection is finished.
  79. */
  80. b->type = &apr_bucket_type_socket;
  81. b->length = (apr_size_t)(-1);
  82. b->start = -1;
  83. b->data = p;
  84. return b;
  85. }
  86. APU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *p,
  87. apr_bucket_alloc_t *list)
  88. {
  89. apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
  90. APR_BUCKET_INIT(b);
  91. b->free = apr_bucket_free;
  92. b->list = list;
  93. return apr_bucket_socket_make(b, p);
  94. }
  95. APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_socket = {
  96. "SOCKET", 5, APR_BUCKET_DATA,
  97. apr_bucket_destroy_noop,
  98. socket_bucket_read,
  99. apr_bucket_setaside_notimpl,
  100. apr_bucket_split_notimpl,
  101. apr_bucket_copy_notimpl
  102. };