comparison src/c/urweb.c @ 1552:c3b5cf5c2f98

Gentle handling of back-button returns to pages with stale message-passing credentials
author Adam Chlipala <adam@chlipala.net>
date Sun, 28 Aug 2011 17:16:54 -0400
parents 5175bed443f9
children e1f5d9c4cc20
comparison
equal deleted inserted replaced
1551:5175bed443f9 1552:c3b5cf5c2f98
245 245
246 void uw_set_on_success(char *s) { 246 void uw_set_on_success(char *s) {
247 on_success = s; 247 on_success = s;
248 } 248 }
249 249
250 static void chastise(int (*send)(int sockfd, const void *buf, ssize_t len), int sock) {
251 send(sock, on_success, strlen(on_success));
252 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
253 send(sock, "R", 1);
254 close(sock);
255 }
256
250 void uw_client_connect(unsigned id, int pass, int sock, 257 void uw_client_connect(unsigned id, int pass, int sock,
251 int (*send)(int sockfd, const void *buf, ssize_t len), 258 int (*send)(int sockfd, const void *buf, ssize_t len),
252 int (*close)(int fd), 259 int (*close)(int fd),
253 void *logger_data, uw_logger log_error) { 260 void *logger_data, uw_logger log_error) {
254 client *c = find_client(id); 261 client *c = find_client(id);
255 262
256 if (c == NULL) { 263 if (c == NULL) {
257 close(sock); 264 chastise(send, sock);
258 log_error(logger_data, "Out-of-bounds client request (%u)\n", id); 265 log_error(logger_data, "Out-of-bounds client request (%u)\n", id);
259 return; 266 return;
260 } 267 }
261 268
262 pthread_mutex_lock(&c->lock); 269 pthread_mutex_lock(&c->lock);
263 270
264 if (c->mode != USED) { 271 if (c->mode != USED) {
265 pthread_mutex_unlock(&c->lock); 272 pthread_mutex_unlock(&c->lock);
266 close(sock); 273 chastise(send, sock);
267 log_error(logger_data, "Client request for unused slot (%u)\n", id); 274 log_error(logger_data, "Client request for unused slot (%u)\n", id);
268 return; 275 return;
269 } 276 }
270 277
271 if (pass != c->pass) { 278 if (pass != c->pass) {
272 pthread_mutex_unlock(&c->lock); 279 pthread_mutex_unlock(&c->lock);
273 close(sock); 280 chastise(send, sock);
274 log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass); 281 log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass);
275 return; 282 return;
276 } 283 }
277 284
278 if (c->sock != -1) { 285 if (c->sock != -1) {
1340 return ""; 1347 return "";
1341 } else { 1348 } else {
1342 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : ""; 1349 char *sig = ctx->needs_sig ? ctx->app->cookie_sig(ctx) : "";
1343 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix) 1350 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->app->url_prefix)
1344 + (ctx->needs_sig ? strlen(sig) + 7 : 0)); 1351 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
1345 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();", 1352 sprintf(r, "isPost=%s;client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
1353 (ctx->isPost ? "true" : "false"),
1346 ctx->client->id, 1354 ctx->client->id,
1347 ctx->client->pass, 1355 ctx->client->pass,
1348 ctx->app->url_prefix, 1356 ctx->app->url_prefix,
1349 ctx->app->timeout, 1357 ctx->app->timeout,
1350 ctx->needs_sig ? "sig=\"" : "", 1358 ctx->needs_sig ? "sig=\"" : "",