1/*
2   Framework for testing with a server process
3   Copyright (C) 2001-2004, Joe Orton <joe@manyfish.co.uk>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19*/
20
21#ifndef CHILD_H
22#define CHILD_H 1
23
24#include "config.h"
25
26#ifdef HAVE_STRING_H
27#include <string.h> /* for strlen() */
28#endif
29
30#include "ne_socket.h"
31
32/* Test which does DNS lookup on "localhost": this must be the first
33 * named test. */
34int lookup_localhost(void);
35
36/* Test which looks up real local hostname. */
37int lookup_hostname(void);
38
39/* set to local hostname if lookup_hostname succeeds. */
40extern char *local_hostname;
41
42/* Callback for spawn_server. */
43typedef int (*server_fn)(ne_socket *sock, void *userdata);
44
45/* Spawns server child process:
46 * - forks child process.
47 * - child process listens on localhost at given port.
48 * - when you connect to it, 'fn' is run...
49 * fn is passed the client/server socket as first argument,
50 * and userdata as second.
51 * - the socket is closed when 'fn' returns, so don't close in in 'fn'.
52 */
53int spawn_server(int port, server_fn fn, void *userdata);
54
55/* Like spawn_server; if bind_local is non-zero, binds server to
56 * localhost, otherwise, binds server to real local hostname.  (must
57 * have called lookup_localhost or lookup_hostname as approprate
58 * beforehand).  */
59int spawn_server_addr(int bind_local, int port, server_fn fn, void *userdata);
60
61/* Like spawn server except will continue accepting connections and
62 * processing requests, up to 'n' times. If 'n' is reached, then the
63 * child process exits with a failure status. */
64int spawn_server_repeat(int port, server_fn fn, void *userdata, int n);
65
66/* Blocks until child process exits, and gives return code of 'fn'. */
67int await_server(void);
68
69/* Kills child process. */
70int reap_server(void);
71
72/* Returns non-zero if server process has already died. */
73int dead_server(void);
74
75/* If discard_request comes across a header called 'want_header', it
76 * will call got_header passing the header field value. */
77extern const char *want_header;
78typedef void (*got_header_fn)(char *value);
79extern got_header_fn got_header;
80
81/* Send string to child; ne_sock_fullwrite with debugging. */
82ssize_t server_send(ne_socket *sock, const char *data, size_t len);
83
84/* Utility macro: send given string down socket. */
85#define SEND_STRING(sock, str) server_send((sock), (str), strlen((str)))
86
87/* Tries to ensure that the socket will be closed using RST rather
88 * than FIN. */
89int reset_socket(ne_socket *sock);
90
91/* Utility function: discard request.  Sets context on error. */
92int discard_request(ne_socket *sock);
93
94/* Utility function: discard request body. Sets context on error. */
95int discard_body(ne_socket *sock);
96
97struct serve_file_args {
98    const char *fname;
99    const char *headers;
100    int chunks;
101};
102
103/* Utility function: callback for spawn_server: pass pointer to
104 * serve_file_args as userdata, and args->fname is served as a 200
105 * request. If args->headers is non-NULL, it must be a set of
106 * CRLF-terminated lines which is added in to the response headers.
107 * If args->chunks is non-zero, the file is delivered using chunks of
108 * that size. */
109int serve_file(ne_socket *sock, void *ud);
110
111/* set to value of C-L header by discard_request. */
112extern int clength;
113
114/* Sleep for a short time. */
115void minisleep(void);
116
117#endif /* CHILD_H */
118