blob: 6bc5915b49c03ea59cf7b43db65e671dc097f0b4 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
2 * Helper functions to send data over a socket and buffer.
3 * Should probably move somewhere else, but where ?
4 *
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
26#include <common/compat.h>
27#include <common/config.h>
28#include <common/debug.h>
29#include <common/memory.h>
30#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020031#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020032#include <common/time.h>
33#include <common/version.h>
34
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <proto/backend.h>
36#include <proto/buffers.h>
37#include <proto/fd.h>
38#include <proto/senddata.h>
39#include <proto/session.h>
40
41/*
42 * returns a message to the client ; the connection is shut down for read,
43 * and the request is cleared so that no server connection can be initiated.
44 * The client must be in a valid state for this (HEADER, DATA ...).
45 * Nothing is performed on the server side. The message is contained in a
46 * "chunk". If it is null, then an empty message is used.
47 * The reply buffer doesn't need to be empty before this.
48 */
49void client_retnclose(struct session *s, const struct chunk *msg)
50{
51 EV_FD_CLR(s->cli_fd, DIR_RD);
52 EV_FD_SET(s->cli_fd, DIR_WR);
53 buffer_shutr(s->req);
Willy Tarreaue393fe22008-08-16 22:18:07 +020054 buffer_flush(s->req);
Willy Tarreau0c303ee2008-07-07 00:09:58 +020055 s->rep->wex = tick_add_ifset(now_ms, s->rep->wto);
Willy Tarreauadfb8562008-08-11 15:24:42 +020056 s->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau1ae3a052008-08-16 10:56:30 +020057 s->cli_state = CL_STSHUTR; // FIXME: still used by unix sockets
Willy Tarreau91861262007-10-17 17:06:05 +020058 buffer_flush(s->rep);
59 if (msg && msg->len)
60 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreau91861262007-10-17 17:06:05 +020061}
62
63
64/*
65 * returns a message into the rep buffer, and flushes the req buffer.
66 * The reply buffer doesn't need to be empty before this. The message
67 * is contained in a "chunk". If it is null, then an empty message is
68 * used.
69 */
70void client_return(struct session *s, const struct chunk *msg)
71{
Willy Tarreaue393fe22008-08-16 22:18:07 +020072 buffer_flush(s->req);
Willy Tarreau91861262007-10-17 17:06:05 +020073 buffer_flush(s->rep);
74 if (msg && msg->len)
75 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreau91861262007-10-17 17:06:05 +020076}
77
78/*
79 * Local variables:
80 * c-indent-level: 8
81 * c-basic-offset: 8
82 * End:
83 */