Пример #1
0
int
serve_file_upload(struct http_request *req)
{
	u_int8_t		*d;
	struct kore_buf		*b;
	struct http_file	*f;
	size_t			len;
	char			*name, buf[BUFSIZ];

	b = kore_buf_alloc(asset_len_upload_html);
	kore_buf_append(b, asset_upload_html, asset_len_upload_html);

	if (req->method == HTTP_METHOD_POST) {
		if (req->http_body_fd != -1)
			kore_log(LOG_NOTICE, "file is on disk");

		http_populate_multipart_form(req);
		if (http_argument_get_string(req, "firstname", &name)) {
			kore_buf_replace_string(b, "$firstname$",
			    name, strlen(name));
		} else {
			kore_buf_replace_string(b, "$firstname$", NULL, 0);
		}

		if ((f = http_file_lookup(req, "file")) != NULL) {
			(void)snprintf(buf, sizeof(buf),
			    "%s is %ld bytes", f->filename, f->length);
			kore_buf_replace_string(b,
			    "$upload$", buf, strlen(buf));
		} else {
			kore_buf_replace_string(b, "$upload$", NULL, 0);
		}
	} else {
		kore_buf_replace_string(b, "$upload$", NULL, 0);
		kore_buf_replace_string(b, "$firstname$", NULL, 0);
	}

	d = kore_buf_release(b, &len);

	http_response_header(req, "content-type", "text/html");
	http_response(req, 200, d, len);
	kore_free(d);

	return (KORE_RESULT_OK);
}
Пример #2
0
int
serve_file_upload(struct http_request *req)
{
    int			r;
    u_int8_t		*d;
    struct kore_buf		*b;
    u_int32_t		len;
    char			*name, buf[BUFSIZ];

    b = kore_buf_create(asset_len_upload_html);
    kore_buf_append(b, asset_upload_html, asset_len_upload_html);

    if (req->method == HTTP_METHOD_POST) {
        http_populate_multipart_form(req, &r);
        if (http_argument_get_string("firstname", &name, &len)) {
            kore_buf_replace_string(b, "$firstname$", name, len);
        } else {
            kore_buf_replace_string(b, "$firstname$", NULL, 0);
        }

        if (http_file_lookup(req, "file", &name, &d, &len)) {
            (void)snprintf(buf, sizeof(buf),
                           "%s is %d bytes", name, len);
            kore_buf_replace_string(b,
                                    "$upload$", buf, strlen(buf));
        } else {
            kore_buf_replace_string(b, "$upload$", NULL, 0);
        }
    } else {
        kore_buf_replace_string(b, "$upload$", NULL, 0);
        kore_buf_replace_string(b, "$firstname$", NULL, 0);
    }

    d = kore_buf_release(b, &len);

    http_response_header(req, "content-type", "text/html");
    http_response(req, 200, d, len);
    kore_mem_free(d);

    return (KORE_RESULT_OK);
}
Пример #3
0
int
page(struct http_request *req)
{
	int			fd;
	struct http_file	*file;
	u_int8_t		buf[BUFSIZ];
	ssize_t			ret, written;

	/* Only deal with POSTs. */
	if (req->method != HTTP_METHOD_POST) {
		http_response(req, 405, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* Parse the multipart data that was present. */
	http_populate_multipart_form(req);

	/* Find our file. */
	if ((file = http_file_lookup(req, "file")) == NULL) {
		http_response(req, 400, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* Open dump file where we will write file contents. */
	fd = open(file->filename, O_CREAT | O_TRUNC | O_WRONLY, 0700);
	if (fd == -1) {
		http_response(req, 500, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* While we have data from http_file_read(), write it. */
	/* Alternatively you could look at file->offset and file->length. */
	ret = KORE_RESULT_ERROR;
	for (;;) {
		ret = http_file_read(file, buf, sizeof(buf));
		if (ret == -1) {
			kore_log(LOG_ERR, "failed to read from file");
			http_response(req, 500, NULL, 0);
			goto cleanup;
		}

		if (ret == 0)
			break;

		written = write(fd, buf, ret);
		if (written == -1) {
			kore_log(LOG_ERR,"write(%s): %s",
			    file->filename, errno_s);
			http_response(req, 500, NULL, 0);
			goto cleanup;
		}

		if (written != ret) {
			kore_log(LOG_ERR, "partial write on %s",
			    file->filename);
			http_response(req, 500, NULL, 0);
			goto cleanup;
		}
	}

	ret = KORE_RESULT_OK;
	http_response(req, 200, NULL, 0);
	kore_log(LOG_INFO, "file '%s' successfully received",
	    file->filename);

cleanup:
	if (close(fd) == -1)
		kore_log(LOG_WARNING, "close(%s): %s", file->filename, errno_s);

	if (ret == KORE_RESULT_ERROR) {
		if (unlink(file->filename) == -1) {
			kore_log(LOG_WARNING, "unlink(%s): %s",
			    file->filename, errno_s);
		}
		ret = KORE_RESULT_OK;
	}

	return (KORE_RESULT_OK);
}