Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Helper functions to send data over a socket and buffer. |
| 3 | * Should probably move somewhere else, but where ? |
| 4 | * |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 6 | * |
| 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 Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 31 | #include <common/ticks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 32 | #include <common/time.h> |
| 33 | #include <common/version.h> |
| 34 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 35 | #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 | */ |
| 49 | void client_retnclose(struct session *s, const struct chunk *msg) |
| 50 | { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 51 | //FIXME: must move to lower level |
| 52 | //EV_FD_CLR(s->cli_fd, DIR_RD); |
| 53 | //EV_FD_SET(s->cli_fd, DIR_WR); |
| 54 | buffer_abort(s->req); |
| 55 | |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 56 | s->cli_state = CL_STSHUTR; // FIXME: still used by unix sockets |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 57 | buffer_flush(s->rep); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 58 | buffer_shutr_now(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 59 | if (msg && msg->len) |
| 60 | buffer_write(s->rep, msg->str, msg->len); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 61 | |
| 62 | s->rep->wex = tick_add_ifset(now_ms, s->rep->wto); |
| 63 | s->rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
| 67 | /* |
| 68 | * returns a message into the rep buffer, and flushes the req buffer. |
| 69 | * The reply buffer doesn't need to be empty before this. The message |
| 70 | * is contained in a "chunk". If it is null, then an empty message is |
| 71 | * used. |
| 72 | */ |
| 73 | void client_return(struct session *s, const struct chunk *msg) |
| 74 | { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 75 | buffer_flush(s->req); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 76 | buffer_flush(s->rep); |
| 77 | if (msg && msg->len) |
| 78 | buffer_write(s->rep, msg->str, msg->len); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Local variables: |
| 83 | * c-indent-level: 8 |
| 84 | * c-basic-offset: 8 |
| 85 | * End: |
| 86 | */ |