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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
|
/*
* Copyright (c) 2014-2019 Paul Mattes.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of Paul Mattes nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* httpd-nodes.c
* x3270 webserver, methods for HTTP objects
*/
#include "globals.h"
#if !defined(_WIN32) /*[*/
# include <unistd.h>
# include <netinet/in.h>
# include <sys/select.h>
# include <arpa/inet.h>
#endif /*]*/
#include <signal.h>
#include <fcntl.h>
#include <assert.h>
#include "fprint_screen.h"
#include "varbuf.h"
#include "httpd-core.h"
#include "httpd-io.h"
#include "httpd-nodes.h"
#if defined(_WIN32) /*[*/
# include "winprint.h"
#endif /*]*/
extern unsigned char favicon[];
extern unsigned favicon_size;
/**
* Capture the screen image.
*
* @param[in] d Session handle
* @param[out] image Image in HTML, if succeeded; must free when finished
* @param[out] status Error code, if failed
*
* @return true for success, false for failure
*/
static bool
hn_image(void *dhandle, varbuf_t *image, httpd_status_t *status)
{
httpd_status_t rv;
int fd;
FILE *f;
char *temp_name;
char buf[8192];
/* Open the temporary file. */
#if defined(_WIN32) /*[*/
fd = win_mkstemp(&temp_name, P_HTML);
#else /*][*/
temp_name = NewString("/tmp/x3hXXXXXX");
fd = mkstemp(temp_name);
#endif /*]*/
if (fd < 0) {
rv = httpd_dyn_error(dhandle, CT_HTML, 400, "Internal error (open)");
unlink(temp_name);
Free(temp_name);
*status = rv;
return false;
}
f = fdopen(fd, "w+");
if (f == NULL) {
rv = httpd_dyn_error(dhandle, CT_HTML, 400, "Internal error (fdopen)");
close(fd);
unlink(temp_name);
Free(temp_name);
*status = rv;
return false;
}
/* Write the screen to it in HTML. */
switch (fprint_screen(f, P_HTML, FPS_NO_HEADER, NULL, NULL, NULL)) {
case FPS_STATUS_SUCCESS:
case FPS_STATUS_SUCCESS_WRITTEN:
break;
case FPS_STATUS_ERROR:
case FPS_STATUS_CANCEL:
rv = httpd_dyn_error(dhandle, CT_HTML, 400,
"Internal error (fprint_screen)");
fclose(f);
unlink(temp_name);
Free(temp_name);
*status = rv;
return false;
case FPS_STATUS_WAIT:
assert(false);
return false;
}
/* Read it back into a varbuf_t. */
fflush(f);
rewind(f);
vb_init(image);
while (fgets(buf, sizeof(buf), f) != NULL) {
vb_appends(image, buf);
}
/* Dispose of the file. */
fclose(f);
unlink(temp_name);
Free(temp_name);
/* Success. */
return true;
}
/**
* Callback for the screen image dynamic node.
*
* @param[in] uri URI
* @param[in] dhandle Session handle
*
* @return httpd_status_t
*/
static httpd_status_t
hn_screen_image(const char *uri, void *dhandle)
{
httpd_status_t rv;
varbuf_t r;
/* Get the image. */
if (hn_image(dhandle, &r, &rv)) {
/* Success: Write the response. */
rv = httpd_dyn_complete(dhandle,
"<head>\n\
<title>3270 Screen Image</title>\n\
</head>\n\
<body>\n\
%.*s\n", (int)vb_len(&r), vb_buf(&r));
vb_free(&r);
}
return rv;
}
/* The tiny HTML form on the interactive page. */
#define CMD_FORM \
"<form method=\"GET\" accept-charset=\"UTF-8\" target=\"_self\">\n\
Action and parameters:<br>\n\
<input type=\"text\" name=\"action\" size=\"50\" autofocus>\n\
<input type=\"submit\" value=\"Submit\">\n\
</form>\n"
/**
* Completion callback for the interactive form.
*
* @param[in] dhandle daemon handle
* @param[in] cbs completion status
* @param[in] buf data buffer
* @param[in] len length of data buffer
* @param[in] sl_buf status-line buffer
* @param[in] sl_len length of status-line buffer
*/
static void
dyn_form_complete(void *dhandle, sendto_cbs_t cbs, const char *buf,
size_t len, const char *sl_buf, size_t sl_len)
{
varbuf_t r;
httpd_status_t rv = HS_CONTINUE;
switch (cbs) {
case SC_SUCCESS:
if (hn_image(dhandle, &r, &rv)) {
if (len) {
rv = httpd_dyn_complete(dhandle,
"<head>\n\
<title>Interactive Form</title>\n\
</head>\n\
<body>\n"
CMD_FORM
"<br>\n\
%s\n\
<h2>Status</h2>\n\
<pre>%.*s</pre>\n\
<h2>Result</h2>\n\
<pre>%.*s</pre>",
vb_buf(&r),
sl_len, sl_buf,
len, buf);
} else {
rv = httpd_dyn_complete(dhandle,
"<head>\n\
<title>Interactive Form</title>\n\
</head>\n\
<body>\n"
CMD_FORM
"<br>\n\
%s\n\
<h2>Status</h2>\n\
<pre>%.*s</pre>\n\
<h2>Result</h2>\n\
<i>(none)</i>",
vb_buf(&r),
sl_len, sl_buf);
}
vb_free(&r);
}
break;
case SC_USER_ERROR:
rv = httpd_dyn_error(dhandle, CT_HTML, 400, "%.*s", len, buf);
break;
case SC_SYSTEM_ERROR:
rv = httpd_dyn_error(dhandle, CT_HTML, 500, "%.*s", len, buf);
break;
}
hio_async_done(dhandle, rv);
}
/**
* Callback for the interactive form dynamic node.
*
* @param[in] uri URI
* @param[in] dhandle Session handle
*
* @return httpd_status_t
*/
static httpd_status_t
hn_interact(const char *uri, void *dhandle)
{
const char *action;
httpd_status_t rv;
varbuf_t r;
/* If the specified an action, execute it. */
action = httpd_fetch_query(dhandle, "action");
if (action && *action) {
switch (hio_to3270(action, dyn_form_complete, dhandle, CT_TEXT)) {
case SENDTO_COMPLETE:
return HS_SUCCESS_OPEN; /* not strictly accurate */
case SENDTO_PENDING:
return HS_PENDING;
case SENDTO_INVALID:
return httpd_dyn_error(dhandle, CT_HTML, 400,
"Invalid 3270 action.\n");
default:
case SENDTO_FAILURE:
return httpd_dyn_error(dhandle, CT_HTML, 500,
"Processing error.\n");
}
}
/* Otherwise, display the empty form. */
if (!hn_image(dhandle, &r, &rv)) {
return rv;
}
rv = httpd_dyn_complete(dhandle,
"<head>\n\
<title>Interactive Form</title>\n\
</head>\n\
<body>\n"
CMD_FORM
"<br>\n\
%s\n",
vb_buf(&r));
vb_free(&r);
return rv;
}
/**
* Completion callback for the 3270 text command node (/3270/rest/text).
*
* @param[in] dhandle Session handle
* @param[in] cbs Request status
* @param[in] buf Result / explanatory text buffer
* @param[in] len Result / explanatory text buffer length
* @param[in] sl_buf Status line buffer (ignored)
* @param[in] sl_len Status line length (ignored)
*/
static void
rest_dyn_text_complete(void *dhandle, sendto_cbs_t cbs, const char *buf,
size_t len, const char *sl_buf, size_t sl_len)
{
httpd_status_t rv = HS_CONTINUE;
switch (cbs) {
case SC_SUCCESS:
rv = httpd_dyn_complete(dhandle, "%.*s", len, buf);
break;
case SC_USER_ERROR:
rv = httpd_dyn_error(dhandle, CT_TEXT, 400, "%.*s", len, buf);
break;
case SC_SYSTEM_ERROR:
rv = httpd_dyn_error(dhandle, CT_TEXT, 400, "%.*s", len, buf);
break;
}
hio_async_done(dhandle, rv);
}
/**
* Callback for the REST API plain-text nonterminal dynamic node
* (/3270/rest/text).
*
* @param[in] url URL fragment
* @param[in] dhandle daemon handle
*
* @return httpd_status_t
*/
static httpd_status_t
rest_text_dyn(const char *url, void *dhandle)
{
if (!*url) {
return httpd_dyn_error(dhandle, CT_TEXT, 400, "Missing 3270 action.\n");
}
switch (hio_to3270(url, rest_dyn_text_complete, dhandle, CT_TEXT)) {
case SENDTO_COMPLETE:
return HS_SUCCESS_OPEN; /* not strictly accurate */
case SENDTO_PENDING:
return HS_PENDING;
case SENDTO_INVALID:
return httpd_dyn_error(dhandle, CT_TEXT, 400, "Invalid 3270 action.\n");
default:
case SENDTO_FAILURE:
return httpd_dyn_error(dhandle, CT_TEXT, 500, "Processing error.\n");
}
}
/**
* Completion callback for the 3270 text command node (/3270/rest/stext).
*
* @param[in] dhandle daemon handle
* @param[in] cbs completion status
* @param[in] buf data buffer
* @param[in] len length of data buffer
* @param[in] sl_buf status-line buffer
* @param[in] sl_len length of status-line buffer
*/
static void
rest_dyn_status_text_complete(void *dhandle, sendto_cbs_t cbs, const char *buf,
size_t len, const char *sl_buf, size_t sl_len)
{
httpd_status_t rv = HS_CONTINUE;
switch (cbs) {
case SC_SUCCESS:
rv = httpd_dyn_complete(dhandle, "%.*s\n%.*s",
sl_len, sl_buf,
len, buf);
break;
case SC_USER_ERROR:
rv = httpd_dyn_error(dhandle, CT_TEXT, 400, "%.*s", len, buf);
break;
case SC_SYSTEM_ERROR:
rv = httpd_dyn_error(dhandle, CT_TEXT, 500, "%.*s", len, buf);
break;
}
hio_async_done(dhandle, rv);
}
/**
* Callback for the REST API plain-text plus status nonterminal dynamic node
* (/3270/rest/stext).
*
* @param[in] url URL fragment
* @param[in] dhandle daemon handle
*
* @return httpd_status_t
*/
static httpd_status_t
rest_status_text_dyn(const char *url, void *dhandle)
{
if (!*url) {
return httpd_dyn_error(dhandle, CT_TEXT, 400, "Missing 3270 action.\n");
}
switch (hio_to3270(url, rest_dyn_status_text_complete, dhandle, CT_TEXT)) {
case SENDTO_COMPLETE:
return HS_SUCCESS_OPEN; /* not strictly accurate */
case SENDTO_PENDING:
return HS_PENDING;
case SENDTO_INVALID:
return httpd_dyn_error(dhandle, CT_TEXT, 400, "Invalid 3270 action.\n");
default:
case SENDTO_FAILURE:
return httpd_dyn_error(dhandle, CT_TEXT, 500, "Processing error.\n");
}
}
/**
* Completion callback for the 3270 html command node (/3270/rest/html).
*
* @param[in] dhandle daemon handle
* @param[in] cbs completion status
* @param[in] buf data buffer
* @param[in] len length of data buffer
* @param[in] sl_buf status-line buffer
* @param[in] sl_len length of status-line buffer
*/
static void
rest_dyn_html_complete(void *dhandle, sendto_cbs_t cbs, const char *buf,
size_t len, const char *sl_buf, size_t sl_len)
{
httpd_status_t rv = HS_CONTINUE;
switch (cbs) {
case SC_SUCCESS:
if (len) {
rv = httpd_dyn_complete(dhandle,
"<head>\n\
<title>Success</title>\n\
</head>\n\
<body>\n\
<h1>Success</h1>\n\
<h2>Status</h2>\n\
<pre>%.*s</pre>\n\
<h2>Result</h2>\n\
<pre>%.*s</pre>",
sl_len, sl_buf,
len, buf);
} else {
rv = httpd_dyn_complete(dhandle,
"<head>\n\
<title>Success</title>\n\
</head>\n\
<body>\n\
<h1>Success</h1>\n\
<h2>Status</h2>\n\
<pre>%.*s</pre>\n\
<h2>Result</h2>\n\
<i>(none)</i>",
sl_len, sl_buf);
}
break;
case SC_USER_ERROR:
rv = httpd_dyn_error(dhandle, CT_HTML, 400, "%.*s", len, buf);
break;
case SC_SYSTEM_ERROR:
rv = httpd_dyn_error(dhandle, CT_HTML, 500, "%.*s", len, buf);
break;
}
hio_async_done(dhandle, rv);
}
/**
* Completion callback for the 3270 json command node (/3270/rest/json).
*
* @param[in] dhandle daemon handle
* @param[in] cbs completion status
* @param[in] buf data buffer
* @param[in] len length of data buffer
* @param[in] sl_buf status-line buffer
* @param[in] sl_len length of status-line buffer
*/
static void
rest_dyn_json_complete(void *dhandle, sendto_cbs_t cbs, const char *buf,
size_t len, const char *sl_buf, size_t sl_len)
{
httpd_status_t rv = HS_CONTINUE;
switch (cbs) {
case SC_SUCCESS:
if (len) {
if (len > 2 && !strcmp(buf + len - 2, ",\n")) {
len -= 2;
}
rv = httpd_dyn_complete(dhandle,
"{\n\
\"status\": \"%.*s\",\n\
\"result\": [\n\
%.*s\n\
]\n\
}\n",
sl_len, sl_buf,
len, buf);
} else {
rv = httpd_dyn_complete(dhandle,
"{\n\
\"status\": \"%.*s\",\n\
\"result\": null\n\
}\n",
sl_len, sl_buf);
}
break;
case SC_USER_ERROR:
rv = httpd_dyn_error(dhandle, CT_JSON, 400, "%.*s", len, buf);
break;
case SC_SYSTEM_ERROR:
rv = httpd_dyn_error(dhandle, CT_JSON, 500, "%.*s", len, buf);
break;
}
hio_async_done(dhandle, rv);
}
/**
* Callback for the REST API HTML nonterminal dynamic node (/3270/rest/html).
*
* @param[in] url URL fragment
* @param[in] dhandle daemon handle
*
* @return httpd_status_t
*/
static httpd_status_t
rest_html_dyn(const char *url, void *dhandle)
{
if (!*url) {
return httpd_dyn_error(dhandle, CT_HTML, 400, "Missing 3270 action.\n");
}
switch (hio_to3270(url, rest_dyn_html_complete, dhandle, CT_HTML)) {
case SENDTO_COMPLETE:
return HS_SUCCESS_OPEN; /* not strictly accurate */
case SENDTO_PENDING:
return HS_PENDING;
case SENDTO_INVALID:
return httpd_dyn_error(dhandle, CT_HTML, 400, "Invalid 3270 action.\n");
default:
case SENDTO_FAILURE:
return httpd_dyn_error(dhandle, CT_HTML, 500, "Processing error.\n");
}
}
/**
* Callback for the REST API JSON nonterminal dynamic node (/3270/rest/json).
*
* @param[in] url URL fragment
* @param[in] dhandle daemon handle
*
* @return httpd_status_t
*/
static httpd_status_t
rest_json_dyn(const char *url, void *dhandle)
{
if (!*url) {
return httpd_dyn_error(dhandle, CT_JSON, 400, "Missing 3270 action.\n");
}
switch (hio_to3270(url, rest_dyn_json_complete, dhandle, CT_JSON)) {
case SENDTO_COMPLETE:
return HS_SUCCESS_OPEN; /* not strictly accurate */
case SENDTO_PENDING:
return HS_PENDING;
case SENDTO_INVALID:
return httpd_dyn_error(dhandle, CT_JSON, 400, "Invalid 3270 action.\n");
default:
case SENDTO_FAILURE:
return httpd_dyn_error(dhandle, CT_JSON, 500, "Processing error.\n");
}
}
/**
* Initialize the HTTP object hierarchy.
*/
void
httpd_objects_init(void)
{
static bool initted = false;
void *nhandle;
if (initted) {
return;
}
initted = true;
httpd_register_dir("/3270", "Emulator state");
httpd_register_dyn_term("/3270/screen.html", "Screen image",
CT_HTML, "text/html; charset=utf-8", HF_TRAILER, hn_screen_image);
httpd_register_dyn_term("/3270/interact.html", "Interactive form",
CT_HTML, "text/html; charset=utf-8", HF_TRAILER, hn_interact);
httpd_register_dir("/3270/rest", "REST interface");
httpd_register_fixed_binary("/favicon.ico", "Browser icon",
CT_BINARY, "image/vnd.microsoft.icon", HF_HIDDEN, favicon,
favicon_size);
nhandle = httpd_register_dyn_nonterm("/3270/rest/text",
"REST plain text interface", CT_TEXT, "text/plain; charset=utf-8",
HF_NONE, rest_text_dyn);
httpd_set_alias(nhandle, "text/Query()");
nhandle = httpd_register_dyn_nonterm("/3270/rest/stext",
"REST plain text interface with status line", CT_TEXT,
"text/plain; charset=utf-8",
HF_NONE, rest_status_text_dyn);
httpd_set_alias(nhandle, "stext/Query()");
nhandle = httpd_register_dyn_nonterm("/3270/rest/html",
"REST HTML interface", CT_HTML, "text/html; charset=utf-8",
HF_TRAILER, rest_html_dyn);
httpd_set_alias(nhandle, "html/Query()");
nhandle = httpd_register_dyn_nonterm("/3270/rest/json",
"REST JSON interface", CT_JSON, "application/json; charset=utf-8",
HF_NONE, rest_json_dyn);
httpd_set_alias(nhandle, "json/Query()");
}
|