summaryrefslogtreecommitdiffstats
path: root/examples/nanohttp/http_server.c
blob: 950a717585da79ac249708821b9de2fbd2b35ea1 (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
191
/******************************************************************
*  $Id: http_server.c,v 1.7 2006/11/23 15:27:33 m0gg Exp $
*
* CSOAP Project:  A http client/server library in C (example)
* Copyright (C) 2003  Ferhat Ayaz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA  02111-1307, USA.
* 
* Email: hero@persua.de
******************************************************************/
#include <stdio.h>
#include <string.h>

#include <nanohttp/nanohttp-common.h>
#include <nanohttp/nanohttp-stream.h>
#include <nanohttp/nanohttp-request.h>
#include <nanohttp/nanohttp-response.h>
#include <nanohttp/nanohttp-server.h>
#include <nanohttp/nanohttp-logging.h>

static int simple_authenticator(struct hrequest_t *req, const char *user, const char *password)
{

	log_info3("logging in user=\"%s\" password=\"%s\"", user, password);

	if (strcmp(user, "bob")) {

		log_warn2("user \"%s\" unkown", user);
		return 0;
	}

	if (strcmp(password, "builder")) {

		log_warn1("wrong password");
		return 0;
	}

	return 1;
}

static void secure_service(httpd_conn_t *conn, struct hrequest_t *req)
{

	httpd_send_header(conn, 200, "OK");
	http_output_stream_write_string(conn->out,
		"<html>"
			"<head>"
				"<title>Secure ressource!</title>"
			"</head>"
			"<body>"
				"<h1>Authenticated access!!!</h1>"
			"</body>"
		"</html>");

	return;
}

static void default_service(httpd_conn_t *conn, struct hrequest_t *req)
{

	httpd_send_header(conn, 404, "Not found");
	http_output_stream_write_string(conn->out,
		"<html>"
			"<head>"
				"<title>Default error page</title>"
			"</head>"
			"<body>"
				"<h1>Default error page</h1>"
				"<div>");

	http_output_stream_write_string(conn->out, req->path);

	http_output_stream_write_string(conn->out, " can not be found"
		       		"</div>"
			"</body>"
		"</html>");

	return;
}	

static void headers_service(httpd_conn_t *conn, struct hrequest_t *req)
{
	hpair_t *walker;

	httpd_send_header(conn, 200, "OK");
	http_output_stream_write_string(conn->out,
		"<html>"
			"<head>"
				"<title>Request headers</title>"
			"</head>"
			"<body>"
				"<h1>Request headers</h1>"
				"<ul>");

	for (walker=req->header; walker; walker=walker->next)
	{
		http_output_stream_write_string(conn->out, "<li>");
		http_output_stream_write_string(conn->out, walker->key);
		http_output_stream_write_string(conn->out, " = ");
		http_output_stream_write_string(conn->out, walker->value);
		http_output_stream_write_string(conn->out, "</li>");
	}

	http_output_stream_write_string(conn->out,
				"</ul>"
			"</body>"
		"</html>");

	return;
}

static void root_service(httpd_conn_t *conn, struct hrequest_t *req)
{
	httpd_send_header(conn, 200, "OK");
	http_output_stream_write_string(conn->out,
		"<html>"
			"<head>"
				"<title>nanoHTTP server examples</title>"
			"</head>"
			"<body>"
				"<h1>nanoHTTP server examples</h1>"
				"<ul>"
					"<li><a href=\"/\">Simple service</a></li>"
					"<li><a href=\"/secure\">Secure service</a> (try: bob/builder)</li>"
					"<li><a href=\"/headers\">Request headers</a></li>"
					"<li><a href=\"/not_existent\">The default service</a></li>"
					"<li><a href=\"/nhttp\">Admin page</a> (try -NHTTPDadmin on the command line)</li>"
				"</ul>"
			"</body>"
		"</html>");

	return;
}

int main(int argc, char **argv)
{
	hlog_set_level(HLOG_INFO);

	if (httpd_init(argc, argv)) {

		fprintf(stderr, "Cannot init httpd");
		return 1;
	}

	if (!httpd_register("/", root_service)) {

		fprintf(stderr, "Cannot register service");
		return 1;
	}

	if (!httpd_register_secure("/secure", secure_service, simple_authenticator)) {

		fprintf(stderr, "Cannot register secure service");
		return 1;
	}

	if (!httpd_register("/headers", headers_service)) {

		fprintf(stderr, "Cannot register headers service");
		return 1;
	}

	if (!httpd_register_default("/error", default_service)) {

		fprintf(stderr, "Cannot register default service");
		return 1;
	}

	if (httpd_run()) {

		fprintf(stderr, "can not run httpd");
		return 1;
	}

	httpd_destroy();

	return 0;
}