2013-01-15 18:22:17 +01:00
|
|
|
/*
|
2013-10-30 23:37:06 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2013-01-15 18:22:17 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "HttpdClient.hxx"
|
|
|
|
#include "HttpdInternal.hxx"
|
2013-10-21 08:42:55 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2013-01-30 09:18:52 +01:00
|
|
|
#include "Page.hxx"
|
2013-01-30 09:08:50 +01:00
|
|
|
#include "IcyMetaDataServer.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/SocketError.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2013-10-02 12:20:36 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2013-01-15 18:22:17 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
HttpdClient::~HttpdClient()
|
|
|
|
{
|
|
|
|
if (state == RESPONSE) {
|
|
|
|
if (current_page != nullptr)
|
2013-01-30 09:18:52 +01:00
|
|
|
current_page->Unref();
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2014-01-04 17:11:22 +01:00
|
|
|
ClearQueue();
|
2013-01-27 22:58:07 +01:00
|
|
|
}
|
2013-01-15 18:22:17 +01:00
|
|
|
|
|
|
|
if (metadata)
|
2013-01-30 09:18:52 +01:00
|
|
|
metadata->Unref();
|
2014-01-06 18:02:57 +01:00
|
|
|
|
|
|
|
if (IsDefined())
|
|
|
|
BufferedSocket::Close();
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpdClient::Close()
|
|
|
|
{
|
2013-12-31 16:32:33 +01:00
|
|
|
httpd.RemoveClient(*this);
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpdClient::LockClose()
|
|
|
|
{
|
2013-12-31 16:32:33 +01:00
|
|
|
const ScopeLock protect(httpd.mutex);
|
2013-01-15 18:22:17 +01:00
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpdClient::BeginResponse()
|
|
|
|
{
|
|
|
|
assert(state != RESPONSE);
|
|
|
|
|
|
|
|
state = RESPONSE;
|
|
|
|
current_page = nullptr;
|
|
|
|
|
2013-10-25 00:32:48 +02:00
|
|
|
if (!head_method)
|
2013-12-31 16:32:33 +01:00
|
|
|
httpd.SendHeader(*this);
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a line of the HTTP request.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
HttpdClient::HandleLine(const char *line)
|
|
|
|
{
|
|
|
|
assert(state != RESPONSE);
|
|
|
|
|
|
|
|
if (state == REQUEST) {
|
2013-10-25 00:32:48 +02:00
|
|
|
if (memcmp(line, "HEAD /", 6) == 0) {
|
|
|
|
line += 6;
|
|
|
|
head_method = true;
|
|
|
|
} else if (memcmp(line, "GET /", 5) == 0) {
|
|
|
|
line += 5;
|
|
|
|
} else {
|
2013-01-15 18:22:17 +01:00
|
|
|
/* only GET is supported */
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(httpd_output_domain,
|
|
|
|
"malformed request line from client");
|
2013-01-15 18:22:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-25 00:32:48 +02:00
|
|
|
line = strchr(line, ' ');
|
2013-10-14 22:00:21 +02:00
|
|
|
if (line == nullptr || memcmp(line + 1, "HTTP/", 5) != 0) {
|
2013-01-15 18:22:17 +01:00
|
|
|
/* HTTP/0.9 without request headers */
|
2013-10-25 00:32:48 +02:00
|
|
|
|
|
|
|
if (head_method)
|
|
|
|
return false;
|
|
|
|
|
2013-01-15 18:22:17 +01:00
|
|
|
BeginResponse();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* after the request line, request headers follow */
|
|
|
|
state = HEADERS;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (*line == 0) {
|
|
|
|
/* empty line: request is finished */
|
2013-10-25 00:32:48 +02:00
|
|
|
|
2013-01-15 18:22:17 +01:00
|
|
|
BeginResponse();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-26 16:32:00 +02:00
|
|
|
if (StringEqualsCaseASCII(line, "Icy-MetaData: 1", 15) ||
|
|
|
|
StringEqualsCaseASCII(line, "Icy-MetaData:1", 14)) {
|
2013-01-15 18:22:17 +01:00
|
|
|
/* Send icy metadata */
|
|
|
|
metadata_requested = metadata_supported;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-21 08:42:55 +02:00
|
|
|
if (StringEqualsCaseASCII(line, "transferMode.dlna.org: Streaming", 32)) {
|
2013-01-15 18:22:17 +01:00
|
|
|
/* Send as dlna */
|
|
|
|
dlna_streaming_requested = true;
|
|
|
|
/* metadata is not supported by dlna streaming, so disable it */
|
|
|
|
metadata_supported = false;
|
|
|
|
metadata_requested = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* expect more request headers */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the status line and response headers to the client.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
HttpdClient::SendResponse()
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
assert(state == RESPONSE);
|
|
|
|
|
|
|
|
if (dlna_streaming_requested) {
|
2013-09-05 09:21:53 +02:00
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"HTTP/1.1 206 OK\r\n"
|
|
|
|
"Content-Type: %s\r\n"
|
|
|
|
"Content-Length: 10000\r\n"
|
|
|
|
"Content-RangeX: 0-1000000/1000000\r\n"
|
|
|
|
"transferMode.dlna.org: Streaming\r\n"
|
|
|
|
"Accept-Ranges: bytes\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"realTimeInfo.dlna.org: DLNA.ORG_TLAG=*\r\n"
|
|
|
|
"contentFeatures.dlna.org: DLNA.ORG_OP=01;DLNA.ORG_CI=0\r\n"
|
|
|
|
"\r\n",
|
2013-12-31 16:32:33 +01:00
|
|
|
httpd.content_type);
|
2013-01-15 18:22:17 +01:00
|
|
|
|
|
|
|
} else if (metadata_requested) {
|
2013-10-19 16:58:45 +02:00
|
|
|
char *metadata_header =
|
2013-12-31 16:32:33 +01:00
|
|
|
icy_server_metadata_header(httpd.name, httpd.genre,
|
|
|
|
httpd.website,
|
|
|
|
httpd.content_type,
|
2013-01-15 18:22:17 +01:00
|
|
|
metaint);
|
|
|
|
|
|
|
|
g_strlcpy(buffer, metadata_header, sizeof(buffer));
|
|
|
|
|
2013-10-19 16:58:45 +02:00
|
|
|
delete[] metadata_header;
|
2013-01-15 18:22:17 +01:00
|
|
|
|
|
|
|
} else { /* revert to a normal HTTP request */
|
2013-09-05 09:21:53 +02:00
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
"Content-Type: %s\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Pragma: no-cache\r\n"
|
|
|
|
"Cache-Control: no-cache, no-store\r\n"
|
|
|
|
"\r\n",
|
2013-12-31 16:32:33 +01:00
|
|
|
httpd.content_type);
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
ssize_t nbytes = SocketMonitor::Write(buffer, strlen(buffer));
|
|
|
|
if (gcc_unlikely(nbytes < 0)) {
|
|
|
|
const SocketErrorMessage msg;
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(httpd_output_domain,
|
|
|
|
"failed to write to client: %s",
|
|
|
|
(const char *)msg);
|
2013-01-15 18:22:17 +01:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-31 16:32:33 +01:00
|
|
|
HttpdClient::HttpdClient(HttpdOutput &_httpd, int _fd, EventLoop &_loop,
|
2013-01-15 18:22:17 +01:00
|
|
|
bool _metadata_supported)
|
2013-01-27 22:58:07 +01:00
|
|
|
:BufferedSocket(_fd, _loop),
|
|
|
|
httpd(_httpd),
|
2013-01-15 18:22:17 +01:00
|
|
|
state(REQUEST),
|
2014-01-04 17:04:21 +01:00
|
|
|
queue_size(0),
|
2013-10-25 00:32:48 +02:00
|
|
|
head_method(false),
|
2013-01-15 18:22:17 +01:00
|
|
|
dlna_streaming_requested(false),
|
|
|
|
metadata_supported(_metadata_supported),
|
|
|
|
metadata_requested(false), metadata_sent(true),
|
|
|
|
metaint(8192), /*TODO: just a std value */
|
|
|
|
metadata(nullptr),
|
|
|
|
metadata_current_position(0), metadata_fill(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-04 17:11:22 +01:00
|
|
|
HttpdClient::ClearQueue()
|
2013-01-15 18:22:17 +01:00
|
|
|
{
|
2014-01-04 17:11:22 +01:00
|
|
|
assert(state == RESPONSE);
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2014-01-04 17:07:24 +01:00
|
|
|
while (!pages.empty()) {
|
|
|
|
Page *page = pages.front();
|
|
|
|
pages.pop();
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(queue_size >= page->size);
|
|
|
|
queue_size -= page->size;
|
|
|
|
#endif
|
|
|
|
|
2013-01-30 09:18:52 +01:00
|
|
|
page->Unref();
|
2014-01-04 17:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(queue_size == 0);
|
2014-01-04 17:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpdClient::CancelQueue()
|
|
|
|
{
|
|
|
|
if (state != RESPONSE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ClearQueue();
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
if (current_page == nullptr)
|
|
|
|
CancelWrite();
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
ssize_t
|
|
|
|
HttpdClient::TryWritePage(const Page &page, size_t position)
|
2013-01-15 18:22:17 +01:00
|
|
|
{
|
2013-01-30 09:18:52 +01:00
|
|
|
assert(position < page.size);
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
return Write(page.data + position, page.size - position);
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
ssize_t
|
|
|
|
HttpdClient::TryWritePageN(const Page &page, size_t position, ssize_t n)
|
2013-01-15 18:22:17 +01:00
|
|
|
{
|
2013-01-27 22:58:07 +01:00
|
|
|
return n >= 0
|
|
|
|
? Write(page.data + position, n)
|
|
|
|
: TryWritePage(page, position);
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
ssize_t
|
2013-01-15 18:22:17 +01:00
|
|
|
HttpdClient::GetBytesTillMetaData() const
|
|
|
|
{
|
|
|
|
if (metadata_requested &&
|
|
|
|
current_page->size - current_position > metaint - metadata_fill)
|
|
|
|
return metaint - metadata_fill;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2013-01-27 22:58:07 +01:00
|
|
|
HttpdClient::TryWrite()
|
2013-01-15 18:22:17 +01:00
|
|
|
{
|
2013-12-31 16:32:33 +01:00
|
|
|
const ScopeLock protect(httpd.mutex);
|
2013-01-15 18:22:17 +01:00
|
|
|
|
|
|
|
assert(state == RESPONSE);
|
|
|
|
|
|
|
|
if (current_page == nullptr) {
|
2013-01-27 22:58:07 +01:00
|
|
|
if (pages.empty()) {
|
|
|
|
/* another thread has removed the event source
|
|
|
|
while this thread was waiting for
|
2013-12-31 16:32:33 +01:00
|
|
|
httpd.mutex */
|
2013-01-27 22:58:07 +01:00
|
|
|
CancelWrite();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-15 18:22:17 +01:00
|
|
|
current_page = pages.front();
|
2014-01-04 17:07:24 +01:00
|
|
|
pages.pop();
|
2013-01-15 18:22:17 +01:00
|
|
|
current_position = 0;
|
2014-01-04 17:04:21 +01:00
|
|
|
|
|
|
|
assert(queue_size >= current_page->size);
|
|
|
|
queue_size -= current_page->size;
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
const ssize_t bytes_to_write = GetBytesTillMetaData();
|
2013-01-15 18:22:17 +01:00
|
|
|
if (bytes_to_write == 0) {
|
|
|
|
if (!metadata_sent) {
|
2013-01-27 22:58:07 +01:00
|
|
|
ssize_t nbytes = TryWritePage(*metadata,
|
|
|
|
metadata_current_position);
|
|
|
|
if (nbytes < 0) {
|
|
|
|
auto e = GetSocketError();
|
|
|
|
if (IsSocketErrorAgain(e))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!IsSocketErrorClosed(e)) {
|
|
|
|
SocketErrorMessage msg(e);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(httpd_output_domain,
|
|
|
|
"failed to write to client: %s",
|
|
|
|
(const char *)msg);
|
2013-01-27 22:58:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
metadata_current_position += nbytes;
|
2013-01-15 18:22:17 +01:00
|
|
|
|
|
|
|
if (metadata->size - metadata_current_position == 0) {
|
|
|
|
metadata_fill = 0;
|
|
|
|
metadata_current_position = 0;
|
|
|
|
metadata_sent = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
guchar empty_data = 0;
|
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
ssize_t nbytes = Write(&empty_data, 1);
|
|
|
|
if (nbytes < 0) {
|
|
|
|
auto e = GetSocketError();
|
|
|
|
if (IsSocketErrorAgain(e))
|
|
|
|
return true;
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
if (!IsSocketErrorClosed(e)) {
|
|
|
|
SocketErrorMessage msg(e);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(httpd_output_domain,
|
|
|
|
"failed to write to client: %s",
|
|
|
|
(const char *)msg);
|
2013-01-27 22:58:07 +01:00
|
|
|
}
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
Close();
|
|
|
|
return false;
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
2013-01-30 09:18:52 +01:00
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
metadata_fill = 0;
|
|
|
|
metadata_current_position = 0;
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-27 22:58:07 +01:00
|
|
|
ssize_t nbytes =
|
|
|
|
TryWritePageN(*current_page, current_position,
|
|
|
|
bytes_to_write);
|
|
|
|
if (nbytes < 0) {
|
|
|
|
auto e = GetSocketError();
|
|
|
|
if (IsSocketErrorAgain(e))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!IsSocketErrorClosed(e)) {
|
|
|
|
SocketErrorMessage msg(e);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(httpd_output_domain,
|
|
|
|
"failed to write to client: %s",
|
|
|
|
(const char *)msg);
|
2013-01-27 22:58:07 +01:00
|
|
|
}
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_position += nbytes;
|
2013-01-15 18:22:17 +01:00
|
|
|
assert(current_position <= current_page->size);
|
|
|
|
|
|
|
|
if (metadata_requested)
|
2013-01-27 22:58:07 +01:00
|
|
|
metadata_fill += nbytes;
|
2013-01-15 18:22:17 +01:00
|
|
|
|
|
|
|
if (current_position >= current_page->size) {
|
2013-01-30 09:18:52 +01:00
|
|
|
current_page->Unref();
|
2013-01-15 18:22:17 +01:00
|
|
|
current_page = nullptr;
|
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
if (pages.empty())
|
2013-01-15 18:22:17 +01:00
|
|
|
/* all pages are sent: remove the
|
|
|
|
event source */
|
2013-01-27 22:58:07 +01:00
|
|
|
CancelWrite();
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
return true;
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-30 09:18:52 +01:00
|
|
|
HttpdClient::PushPage(Page *page)
|
2013-01-15 18:22:17 +01:00
|
|
|
{
|
|
|
|
if (state != RESPONSE)
|
|
|
|
/* the client is still writing the HTTP request */
|
|
|
|
return;
|
|
|
|
|
2014-01-04 17:42:03 +01:00
|
|
|
if (queue_size > 256 * 1024) {
|
|
|
|
FormatDebug(httpd_output_domain,
|
|
|
|
"client is too slow, flushing its queue");
|
|
|
|
ClearQueue();
|
|
|
|
}
|
|
|
|
|
2013-01-30 09:18:52 +01:00
|
|
|
page->Ref();
|
2014-01-04 17:07:24 +01:00
|
|
|
pages.push(page);
|
2014-01-04 17:04:21 +01:00
|
|
|
queue_size += page->size;
|
2013-01-15 18:22:17 +01:00
|
|
|
|
2013-01-27 22:58:07 +01:00
|
|
|
ScheduleWrite();
|
2013-01-15 18:22:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-30 09:18:52 +01:00
|
|
|
HttpdClient::PushMetaData(Page *page)
|
2013-01-15 18:22:17 +01:00
|
|
|
{
|
|
|
|
if (metadata) {
|
2013-01-30 09:18:52 +01:00
|
|
|
metadata->Unref();
|
2013-01-15 18:22:17 +01:00
|
|
|
metadata = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_return_if_fail (page);
|
|
|
|
|
2013-01-30 09:18:52 +01:00
|
|
|
page->Ref();
|
2013-01-15 18:22:17 +01:00
|
|
|
metadata = page;
|
|
|
|
metadata_sent = false;
|
|
|
|
}
|
2013-01-27 22:58:07 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
HttpdClient::OnSocketReady(unsigned flags)
|
|
|
|
{
|
|
|
|
if (!BufferedSocket::OnSocketReady(flags))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (flags & WRITE)
|
|
|
|
if (!TryWrite())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferedSocket::InputResult
|
2013-10-15 22:18:37 +02:00
|
|
|
HttpdClient::OnSocketInput(void *data, size_t length)
|
2013-01-27 22:58:07 +01:00
|
|
|
{
|
|
|
|
if (state == RESPONSE) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(httpd_output_domain,
|
|
|
|
"unexpected input from client");
|
2013-01-27 22:58:07 +01:00
|
|
|
LockClose();
|
|
|
|
return InputResult::CLOSED;
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:18:37 +02:00
|
|
|
char *line = (char *)data;
|
|
|
|
char *newline = (char *)memchr(line, '\n', length);
|
2013-01-27 22:58:07 +01:00
|
|
|
if (newline == nullptr)
|
|
|
|
return InputResult::MORE;
|
|
|
|
|
|
|
|
ConsumeInput(newline + 1 - line);
|
|
|
|
|
|
|
|
if (newline > line && newline[-1] == '\r')
|
|
|
|
--newline;
|
|
|
|
|
2013-10-15 22:18:37 +02:00
|
|
|
/* terminate the string at the end of the line */
|
|
|
|
*newline = 0;
|
2013-01-27 22:58:07 +01:00
|
|
|
|
|
|
|
if (!HandleLine(line)) {
|
|
|
|
LockClose();
|
|
|
|
return InputResult::CLOSED;
|
|
|
|
}
|
|
|
|
|
2013-10-25 00:32:48 +02:00
|
|
|
if (state == RESPONSE) {
|
|
|
|
if (!SendResponse())
|
|
|
|
return InputResult::CLOSED;
|
|
|
|
|
|
|
|
if (head_method) {
|
|
|
|
LockClose();
|
|
|
|
return InputResult::CLOSED;
|
|
|
|
}
|
|
|
|
}
|
2013-01-27 22:58:07 +01:00
|
|
|
|
|
|
|
return InputResult::AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-10 18:02:44 +02:00
|
|
|
HttpdClient::OnSocketError(Error &&error)
|
2013-01-27 22:58:07 +01:00
|
|
|
{
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2013-01-27 22:58:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpdClient::OnSocketClosed()
|
|
|
|
{
|
|
|
|
LockClose();
|
|
|
|
}
|