Commit 12ab5564 authored by Max Kellermann's avatar Max Kellermann

event/BufferedSocket: pass writable pointer to OnSocketInput()

Remove the const_cast from HttpdClient.cxx, and avoid one allocation in ClientRead.cxx.
parent 509f8dab
......@@ -111,8 +111,7 @@ public:
private:
/* virtual methods from class BufferedSocket */
virtual InputResult OnSocketInput(const void *data,
size_t length) override;
virtual InputResult OnSocketInput(void *data, size_t length) override;
virtual void OnSocketError(Error &&error) override;
virtual void OnSocketClosed() override;
......
......@@ -22,27 +22,25 @@
#include "Main.hxx"
#include "event/Loop.hxx"
#include <glib.h>
#include <assert.h>
#include <string.h>
BufferedSocket::InputResult
Client::OnSocketInput(const void *data, size_t length)
Client::OnSocketInput(void *data, size_t length)
{
const char *p = (const char *)data;
const char *newline = (const char *)memchr(p, '\n', length);
char *p = (char *)data;
char *newline = (char *)memchr(p, '\n', length);
if (newline == NULL)
return InputResult::MORE;
TimeoutMonitor::ScheduleSeconds(client_timeout);
char *line = g_strndup(p, newline - p);
BufferedSocket::ConsumeInput(newline + 1 - p);
/* terminate the string at the end of the line */
*newline = 0;
enum command_return result = client_process_line(this, line);
g_free(line);
BufferedSocket::ConsumeInput(newline + 1 - p);
enum command_return result = client_process_line(this, p);
switch (result) {
case COMMAND_RETURN_OK:
case COMMAND_RETURN_IDLE:
......
......@@ -101,7 +101,15 @@ protected:
CLOSED,
};
virtual InputResult OnSocketInput(const void *data, size_t length) = 0;
/**
* Data has been received on the socket.
*
* @param data a pointer to the beginning of the buffer; the
* buffer may be modified by the method while it processes the
* data
*/
virtual InputResult OnSocketInput(void *data, size_t length) = 0;
virtual void OnSocketError(Error &&error) = 0;
virtual void OnSocketClosed() = 0;
......
......@@ -402,7 +402,7 @@ HttpdClient::OnSocketReady(unsigned flags)
}
BufferedSocket::InputResult
HttpdClient::OnSocketInput(const void *data, size_t length)
HttpdClient::OnSocketInput(void *data, size_t length)
{
if (state == RESPONSE) {
LogWarning(httpd_output_domain,
......@@ -411,8 +411,8 @@ HttpdClient::OnSocketInput(const void *data, size_t length)
return InputResult::CLOSED;
}
const char *line = (const char *)data;
const char *newline = (const char *)memchr(line, '\n', length);
char *line = (char *)data;
char *newline = (char *)memchr(line, '\n', length);
if (newline == nullptr)
return InputResult::MORE;
......@@ -421,9 +421,8 @@ HttpdClient::OnSocketInput(const void *data, size_t length)
if (newline > line && newline[-1] == '\r')
--newline;
/* terminate the string at the end of the line; the const_cast
is a dirty hack */
*const_cast<char *>(newline) = 0;
/* terminate the string at the end of the line */
*newline = 0;
if (!HandleLine(line)) {
assert(state == RESPONSE);
......
......@@ -177,8 +177,7 @@ public:
protected:
virtual bool OnSocketReady(unsigned flags) override;
virtual InputResult OnSocketInput(const void *data,
size_t length) override;
virtual InputResult OnSocketInput(void *data, size_t length) override;
virtual void OnSocketError(Error &&error) override;
virtual void OnSocketClosed() override;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment