| 1935 | |
| 1936 | |
| 1937 | Future<Nothing> receive( |
| 1938 | network::Socket socket, |
| 1939 | std::function<Future<Response>(const Request&)>&& f, |
| 1940 | Queue<Option<Item>> pipeline) |
| 1941 | { |
| 1942 | // Get the peer address to augment any requests we receive. |
| 1943 | Try<network::Address> address = socket.peer(); |
| 1944 | |
| 1945 | if (address.isError()) { |
| 1946 | return Failure("Failed to get peer address: " + address.error()); |
| 1947 | } |
| 1948 | |
| 1949 | const size_t size = io::BUFFERED_READ_SIZE; |
| 1950 | char* data = new char[size]; |
| 1951 | |
| 1952 | StreamingRequestDecoder* decoder = new StreamingRequestDecoder(); |
| 1953 | |
| 1954 | return loop( |
| 1955 | [=]() { |
| 1956 | return socket.recv(data, size); |
| 1957 | }, |
| 1958 | [=](size_t length) mutable -> Future<ControlFlow<Nothing>> { |
| 1959 | if (length == 0) { |
| 1960 | return Break(); |
| 1961 | } |
| 1962 | |
| 1963 | // Decode as much of the data as possible into HTTP requests. |
| 1964 | const deque<Request*> requests = decoder->decode(data, length); |
| 1965 | |
| 1966 | // NOTE: it's possible the decoder has failed but some |
| 1967 | // requests might be available, i.e., `requests.empty()` is |
| 1968 | // not true, so we wait to return a `Failure` until when there |
| 1969 | // are no requests. |
| 1970 | |
| 1971 | if (decoder->failed() && requests.empty()) { |
| 1972 | return Failure("Decoder error while receiving"); |
| 1973 | } |
| 1974 | |
| 1975 | foreach (Request* request, requests) { |
| 1976 | request->client = address.get(); |
| 1977 | // TODO(benh): To support HTTP pipelining we invoke `f` |
| 1978 | // regardless of whether the previous response has been |
| 1979 | // completed. This can make handling of requests more |
| 1980 | // difficult so we could consider supporting disabling HTTP |
| 1981 | // pipelining via some sort of "options" initially passed |
| 1982 | // in. |
| 1983 | pipeline.put(Item{request, f(*request)}); |
| 1984 | } |
| 1985 | |
| 1986 | return Continue(); // Keep looping! |
| 1987 | }) |
| 1988 | .onAny([=]() { |
| 1989 | delete decoder; |
| 1990 | delete[] data; |
| 1991 | }); |
| 1992 | } |
| 1993 | |
| 1994 |
no test coverage detected