summaryrefslogtreecommitdiff
path: root/tests/revserver.c
blob: 9be0cdf309b0f4a108696d3adfbd67ebed9ef8d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <libsoup/soup-address.h>
#include <libsoup/soup-socket.h>

#include <glib/gthread.h>

static void rev_read (SoupSocket *sock, GString *buf);
static void rev_write (SoupSocket *sock, GString *buf);

static void
reverse (GString *buf)
{
	char tmp, *a, *b;

	a = buf->str;
	b = buf->str + buf->len - 1;

	while (isspace ((unsigned char)*b) && b > a)
		b--;

	while (a < b) {
		tmp = *a;
		*a++ = *b;
		*b-- = tmp;
	}
}

static void
rev_done (SoupSocket *sock, GString *buf)
{
	g_object_unref (sock);
	g_string_free (buf, TRUE);
}

static void
rev_write (SoupSocket *sock, GString *buf)
{
	SoupSocketIOStatus status;
	gsize nwrote;

	do {
		status = soup_socket_write (sock, buf->str, buf->len,
					    &nwrote, NULL);
		memmove (buf->str, buf->str + nwrote, buf->len - nwrote);
		buf->len -= nwrote;
	} while (status == SOUP_SOCKET_OK && buf->len);

	switch (status) {
	case SOUP_SOCKET_OK:
		rev_read (sock, buf);
		break;

	case SOUP_SOCKET_WOULD_BLOCK:
		g_error ("Can't happen");
		break;

	default:
		g_warning ("Socket error");
		/* fall through */

	case SOUP_SOCKET_EOF:
		rev_done (sock, buf);
		break;
	}
}

static void
rev_read (SoupSocket *sock, GString *buf)
{
	SoupSocketIOStatus status;
	char tmp[10];
	gsize nread;
	gboolean eol;

	do {
		status = soup_socket_read_until (sock, tmp, sizeof (tmp),
						 "\n", 1, &nread, &eol, NULL);
		if (status == SOUP_SOCKET_OK)
			g_string_append_len (buf, tmp, nread);
	} while (status == SOUP_SOCKET_OK && !eol);

	switch (status) {
	case SOUP_SOCKET_OK:
		reverse (buf);
		rev_write (sock, buf);
		break;

	case SOUP_SOCKET_WOULD_BLOCK:
		g_error ("Can't happen");
		break;

	default:
		g_warning ("Socket error");
		/* fall through */

	case SOUP_SOCKET_EOF:
		rev_done (sock, buf);
		break;
	}
}

static void *
start_thread (void *client)
{
	rev_read (client, g_string_new (NULL));

	return NULL;
}

static void
new_connection (SoupSocket *listener, SoupSocket *client, gpointer user_data)
{
	GThread *thread;
	GError *error = NULL;

	g_object_ref (client);
	g_object_set (G_OBJECT (client),
		      SOUP_SOCKET_FLAG_NONBLOCKING, FALSE,
		      NULL);

	thread = g_thread_create (start_thread, client, FALSE, &error);
	if (thread == NULL) {
		g_warning ("Could not start thread: %s", error->message);
		g_error_free (error);
		g_object_unref (client);
	}
}

int
main (int argc, char **argv)
{
	SoupSocket *listener;
	SoupAddressFamily family = SOUP_ADDRESS_FAMILY_IPV4;
	guint port = SOUP_ADDRESS_ANY_PORT;
	SoupAddress *addr;
	GMainLoop *loop;
	int opt;

	g_type_init ();
	g_thread_init (NULL);

	while ((opt = getopt (argc, argv, "6p:")) != -1) {
		switch (opt) {
		case '6':
			family = SOUP_ADDRESS_FAMILY_IPV6;
			break;
		case 'p':
			port = atoi (optarg);
			break;
		default:
			fprintf (stderr, "Usage: %s [-6] [-p port]\n",
				 argv[0]);
			exit (1);
		}
	}

	addr = soup_address_new_any (family, port);
	if (!addr) {
		fprintf (stderr, "Could not create listener address\n");
		exit (1);
	}

	listener = soup_socket_new (SOUP_SOCKET_LOCAL_ADDRESS, addr,
				    NULL);
	g_object_unref (addr);
	if (!listener || !soup_socket_listen (listener)) {
		fprintf (stderr, "Could not create listening socket\n");
		exit (1);
	}
	g_signal_connect (listener, "new_connection",
			  G_CALLBACK (new_connection), NULL);
	printf ("Listening on port %d\n",
		soup_address_get_port (
			soup_socket_get_local_address (listener)));

	loop = g_main_loop_new (NULL, TRUE);
	g_main_loop_run (loop);

	g_object_unref (listener);
	return 0;
}