/* * Receive a request to open a new connection. */ int open_connection(struct http_request *req) { char *host, *port; /* Don't want to deal with SPDY connections. */ if (req->owner->proto != CONN_PROTO_HTTP) { http_response(req, HTTP_STATUS_BAD_REQUEST, NULL, 0); return (KORE_RESULT_OK); } /* Parse the query string and grab our arguments. */ http_populate_arguments(req); if (!http_argument_get_string("host", &host, NULL) || !http_argument_get_string("port", &port, NULL)) { http_response(req, HTTP_STATUS_BAD_REQUEST, NULL, 0); return (KORE_RESULT_OK); } /* Create our tunnel. */ if (!ktunnel_pipe_create(req->owner, host, port)) { http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0); return (KORE_RESULT_OK); } /* * Hack so http_response() doesn't end up queueing a new * netbuf for receiving more HTTP requests on the same connection. */ req->owner->flags |= CONN_CLOSE_EMPTY; /* Respond to the client now that we're good to go. */ http_response(req, HTTP_STATUS_OK, NULL, 0); /* Unset this so we don't disconnect after returning. */ req->owner->flags &= ~CONN_CLOSE_EMPTY; return (KORE_RESULT_OK); }
int post_back(struct http_request *req) { u_int32_t len; char *user; if (req->method != HTTP_METHOD_POST) { http_response(req, 500, NULL, 0); return (KORE_RESULT_OK); } http_populate_arguments(req); if (!http_argument_get_string("user", &user, &len)) { http_response(req, 500, NULL, 0); return (KORE_RESULT_OK); } /* Simply echo the supplied user argument back. */ http_response(req, 200, user, len); return (KORE_RESULT_OK); }
int serve_params_test(struct http_request *req) { struct kore_buf *b; u_int8_t *d; u_int32_t len; int r, i; char *test, name[10]; http_populate_arguments(req); b = kore_buf_create(asset_len_params_html); kore_buf_append(b, asset_params_html, asset_len_params_html); /* * The GET parameters will be filtered out on POST. */ if (http_argument_get_string("arg1", &test, &len)) { kore_buf_replace_string(b, "$arg1$", test, len); } else { kore_buf_replace_string(b, "$arg1$", NULL, 0); } if (http_argument_get_string("arg2", &test, &len)) { kore_buf_replace_string(b, "$arg2$", test, len); } else { kore_buf_replace_string(b, "$arg2$", NULL, 0); } if (req->method == HTTP_METHOD_GET) { kore_buf_replace_string(b, "$test1$", NULL, 0); kore_buf_replace_string(b, "$test2$", NULL, 0); kore_buf_replace_string(b, "$test3$", NULL, 0); if (http_argument_get_uint16("id", &r)) kore_log(LOG_NOTICE, "id: %d", r); else kore_log(LOG_NOTICE, "No id set"); http_response_header(req, "content-type", "text/html"); d = kore_buf_release(b, &len); http_response(req, 200, d, len); kore_mem_free(d); return (KORE_RESULT_OK); } for (i = 1; i < 4; i++) { (void)snprintf(name, sizeof(name), "test%d", i); if (http_argument_get_string(name, &test, &len)) { (void)snprintf(name, sizeof(name), "$test%d$", i); kore_buf_replace_string(b, name, test, len); } else { (void)snprintf(name, sizeof(name), "$test%d$", i); kore_buf_replace_string(b, name, NULL, 0); } } http_response_header(req, "content-type", "text/html"); d = kore_buf_release(b, &len); http_response(req, 200, d, len); kore_mem_free(d); return (KORE_RESULT_OK); }
int page_handler(struct http_request *req) { u_int32_t len; char *user, result[64]; /* * Lets check if a task has been created yet, this is important * as we only want to fire this off once and we will be called * again once it has been created. */ if (req->task == NULL) { /* Grab the user argument */ http_populate_arguments(req); if (!http_argument_get_string("user", &user, &len)) { http_response(req, 500, "ERROR\n", 6); return (KORE_RESULT_OK); } /* * Create a new task that will execute the run_curl() * function and bind it to our request. * * Binding a task to a request means Kore will reschedule * the page handler for that request to refire after the * task has completed or when it writes on the task channel. */ kore_task_create(&req->task, run_curl); kore_task_bind_request(req->task, req); /* * Start the task and write the user we received in our * GET request to its channel. */ kore_task_run(req->task); kore_task_channel_write(req->task, user, len); /* * Tell Kore to retry us later. */ return (KORE_RESULT_RETRY); } /* * Our page handler is scheduled to be called when either the * task finishes or has written data onto the channel. * * In order to distuingish between the two we can inspect the * state of the task. */ if (kore_task_state(req->task) != KORE_TASK_STATE_FINISHED) { http_request_sleep(req); return (KORE_RESULT_RETRY); } /* * Task is finished, check the result. */ if (kore_task_result(req->task) != KORE_RESULT_OK) { kore_task_destroy(req->task); http_response(req, 500, NULL, 0); return (KORE_RESULT_OK); } /* * Lets read what our task has written to the channel. * * kore_task_channel_read() will return the amount of bytes * that it received for that read. If the returned bytes is * larger then the buffer you passed this is a sign of truncation * and should be treated carefully. */ len = kore_task_channel_read(req->task, result, sizeof(result)); if (len > sizeof(result)) { http_response(req, 500, NULL, 0); } else { http_response(req, 200, result, len); } /* We good, destroy the task. */ kore_task_destroy(req->task); return (KORE_RESULT_OK); }