static int accept_sub(SLNSessionRef const session, strarg_t const knownURI, HTTPConnectionRef const conn, HTTPHeadersRef const headers) { strarg_t const type = HTTPHeadersGet(headers, "Content-Type"); if(!type) return 415; // Unsupported Media Type SLNSubmissionRef sub = NULL; int rc = SLNSubmissionCreate(session, knownURI, type, &sub); if(rc < 0) goto cleanup; for(;;) { uv_buf_t buf[1] = {}; rc = HTTPConnectionReadBody(conn, buf); if(rc < 0) goto cleanup; if(0 == buf->len) break; rc = SLNSubmissionWrite(sub, (byte_t const *)buf->base, buf->len); if(rc < 0) goto cleanup; } rc = SLNSubmissionEnd(sub); if(rc < 0) goto cleanup; rc = SLNSubmissionStoreBatch(&sub, 1); if(rc < 0) goto cleanup; strarg_t const location = SLNSubmissionGetPrimaryURI(sub); if(!location) rc = UV_ENOMEM; if(rc < 0) goto cleanup; created(location, conn); cleanup: SLNSubmissionFree(&sub); if(UV_EACCES == rc) return 403; // Forbidden if(UV_UNKNOWN == rc) return 400; // Bad Request if(SLN_HASHMISMATCH == rc) return 409; // Conflict if(rc < 0) return 500; return 0; }
int SLNSubmissionWriteFrom(SLNSubmissionRef const sub, ssize_t (*read)(void *, byte_t const **), void *const context) { if(!sub) return 0; assert(read); for(;;) { byte_t const *buf = NULL; ssize_t const len = read(context, &buf); if(0 == len) break; if(len < 0) return (int)len; int rc = SLNSubmissionWrite(sub, buf, len); if(rc < 0) return rc; } return SLNSubmissionEnd(sub); }
static int POST_post(BlogRef const blog, SLNSessionRef const session, HTTPConnectionRef const conn, HTTPMethod const method, strarg_t const URI, HTTPHeadersRef const headers) { if(HTTP_POST != method) return -1; if(0 != uripathcmp("/post", URI, NULL)) return -1; // TODO: CSRF token strarg_t const formtype = HTTPHeadersGet(headers, "content-type"); uv_buf_t boundary[1]; int rc = MultipartBoundaryFromType(formtype, boundary); if(rc < 0) return 400; MultipartFormRef form = NULL; rc = MultipartFormCreate(conn, boundary, &form); if(rc < 0) { return 500; } SLNSubmissionRef sub = NULL; SLNSubmissionRef meta = NULL; str_t *title = NULL; rc = parse_file(blog, session, form, &sub, &meta, &title); if(UV_EACCES == rc) { MultipartFormFree(&form); return 403; } if(rc < 0) { MultipartFormFree(&form); return 500; } SLNSubmissionRef extra = NULL; yajl_gen json = NULL; str_t *target_QSEscaped = NULL; str_t *location = NULL; strarg_t const target = SLNSubmissionGetPrimaryURI(sub); if(!target) rc = UV_ENOMEM; if(rc < 0) goto cleanup; target_QSEscaped = QSEscape(target, strlen(target), true); if(!target_QSEscaped) rc = UV_ENOMEM; if(rc < 0) goto cleanup; rc = SLNSubmissionCreate(session, NULL, target, &extra); if(rc < 0) goto cleanup; rc = SLNSubmissionSetType(extra, SLN_META_TYPE); if(rc < 0) goto cleanup; SLNSubmissionWrite(extra, (byte_t const *)target, strlen(target)); SLNSubmissionWrite(extra, (byte_t const *)STR_LEN("\n\n")); json = yajl_gen_alloc(NULL); if(!json) rc = UV_ENOMEM; if(rc < 0) goto cleanup; yajl_gen_config(json, yajl_gen_print_callback, (void (*)())SLNSubmissionWrite, extra); yajl_gen_config(json, yajl_gen_beautify, (int)true); yajl_gen_map_open(json); if(title) { yajl_gen_string(json, (unsigned char const *)STR_LEN("title")); yajl_gen_string(json, (unsigned char const *)title, strlen(title)); } // TODO: Comment or description? strarg_t const username = SLNSessionGetUsername(session); if(username) { yajl_gen_string(json, (unsigned char const *)STR_LEN("submitter-name")); yajl_gen_string(json, (unsigned char const *)username, strlen(username)); } strarg_t const reponame = SLNRepoGetName(blog->repo); if(reponame) { yajl_gen_string(json, (unsigned char const *)STR_LEN("submitter-repo")); yajl_gen_string(json, (unsigned char const *)reponame, strlen(reponame)); } time_t const now = time(NULL); struct tm t[1]; gmtime_r(&now, t); // TODO: Error checking? str_t tstr[31+1]; size_t const tlen = strftime(tstr, sizeof(tstr), "%FT%TZ", t); // ISO 8601 if(tlen) { yajl_gen_string(json, (unsigned char const *)STR_LEN("submission-time")); yajl_gen_string(json, (unsigned char const *)tstr, tlen); } yajl_gen_string(json, (unsigned char const *)STR_LEN("submission-software")); yajl_gen_string(json, (unsigned char const *)STR_LEN("StrongLink Blog")); str_t *fulltext = aasprintf("%s\n%s", title ?: "", NULL ?: ""); // TODO: Description, GNU-ism if(fulltext) { yajl_gen_string(json, (unsigned char const *)STR_LEN("fulltext")); yajl_gen_string(json, (unsigned char const *)fulltext, strlen(fulltext)); } FREE(&fulltext); yajl_gen_map_close(json); rc = SLNSubmissionEnd(extra); if(rc < 0) goto cleanup; SLNSubmissionRef subs[] = { sub, meta, extra }; rc = SLNSubmissionStoreBatch(subs, numberof(subs)); location = aasprintf("/?q=%s", target_QSEscaped); if(!location) rc = UV_ENOMEM; if(rc < 0) goto cleanup; HTTPConnectionSendRedirect(conn, 303, location); cleanup: if(json) { yajl_gen_free(json); json = NULL; } FREE(&title); SLNSubmissionFree(&sub); SLNSubmissionFree(&meta); SLNSubmissionFree(&extra); MultipartFormFree(&form); FREE(&target_QSEscaped); FREE(&location); if(rc < 0) return 500; return 0; }
static int parse_file(BlogRef const blog, SLNSessionRef const session, MultipartFormRef const form, SLNSubmissionRef *const outfile, SLNSubmissionRef *const outmeta, str_t **const outname) { assert(form); SLNSubmissionRef file = NULL; SLNSubmissionRef meta = NULL; SLNFileInfo src[1] = {}; str_t *htmlpath = NULL; int rc = 0; strarg_t const fields[] = { "content-type", "content-disposition", }; char content_type[100]; char content_disposition[1024]; uv_buf_t values[] = { uv_buf_init(BUF_LEN(content_type)), uv_buf_init(BUF_LEN(content_disposition)), }; assert(numberof(fields) == numberof(values)); rc = MultipartFormReadHeadersStatic(form, values, fields, numberof(values)); if(rc < 0) goto cleanup; strarg_t type; if(0 == strcmp("form-data; name=\"markdown\"", content_disposition)) { type = "text/markdown; charset=utf-8"; } else { type = content_type; static strarg_t const f[] = { "filename", "filename*" }; str_t *v[numberof(f)] = {}; ContentDispositionParse(content_disposition, NULL, v, f, numberof(f)); if(v[1]) { *outname = v[1]; v[1] = NULL; } else { *outname = v[0]; v[0] = NULL; } for(size_t i = 0; i < numberof(v); i++) FREE(&v[i]); } rc = SLNSubmissionCreate(session, NULL, NULL, &file); if(rc < 0) goto cleanup; rc = SLNSubmissionSetType(file, type); if(rc < 0) goto cleanup; for(;;) { uv_buf_t buf[1]; rc = MultipartFormReadData(form, buf); if(rc < 0) goto cleanup; if(0 == buf->len) break; rc = SLNSubmissionWrite(file, (byte_t const *)buf->base, buf->len); if(rc < 0) goto cleanup; } rc = SLNSubmissionEnd(file); if(rc < 0) goto cleanup; rc = SLNSubmissionGetFileInfo(file, src); if(rc < 0) goto cleanup; htmlpath = BlogCopyPreviewPath(blog, src->hash); if(!htmlpath) rc = UV_ENOMEM; if(rc < 0) goto cleanup; strarg_t const URI = SLNSubmissionGetPrimaryURI(file); (void)BlogConvert(blog, session, htmlpath, &meta, URI, src); // We don't actually care about failure here? // Even if no preview and no meta-file can be generated, that's fine. *outfile = file; file = NULL; *outmeta = meta; meta = NULL; cleanup: SLNSubmissionFree(&file); SLNSubmissionFree(&meta); SLNFileInfoCleanup(src); FREE(&htmlpath); return rc; }
static int import(SLNPullRef const pull, strarg_t const URI, size_t const pos, HTTPConnectionRef *const conn) { if(!pull) return 0; // TODO: Even if there's nothing to do, we have to enqueue something to fill up our reserved slots. I guess it's better than doing a lot of work inside the connection lock, but there's got to be a better way. SLNSubmissionRef sub = NULL; HTTPHeadersRef headers = NULL; if(!URI) goto enqueue; str_t algo[SLN_ALGO_SIZE]; str_t hash[SLN_HASH_SIZE]; if(SLNParseURI(URI, algo, hash) < 0) goto enqueue; int rc = SLNSessionGetFileInfo(pull->session, URI, NULL); if(rc >= 0) goto enqueue; db_assertf(DB_NOTFOUND == rc, "Database error: %s", sln_strerror(rc)); // TODO: We're logging out of order when we do it like this... // alogf("Pulling %s\n", URI); if(!*conn) { rc = HTTPConnectionCreateOutgoing(pull->host, 0, conn); if(rc < 0) { alogf("Pull import connection error: %s\n", sln_strerror(rc)); goto fail; } } str_t *path = aasprintf("/sln/file/%s/%s", algo, hash); if(!path) { alogf("Pull aasprintf error\n"); goto fail; } rc = HTTPConnectionWriteRequest(*conn, HTTP_GET, path, pull->host); assert(rc >= 0); // TODO FREE(&path); HTTPConnectionWriteHeader(*conn, "Cookie", pull->cookie); HTTPConnectionBeginBody(*conn); rc = HTTPConnectionEnd(*conn); if(rc < 0) { alogf("Pull import request error: %s\n", sln_strerror(rc)); goto fail; } int const status = HTTPConnectionReadResponseStatus(*conn); if(status < 0) { alogf("Pull import response error: %s\n", sln_strerror(status)); goto fail; } if(status < 200 || status >= 300) { alogf("Pull import status error: %d\n", status); goto fail; } rc = HTTPHeadersCreateFromConnection(*conn, &headers); assert(rc >= 0); // TODO /* if(rc < 0) { alogf("Pull import headers error %s\n", sln_strerror(rc)); goto fail; }*/ strarg_t const type = HTTPHeadersGet(headers, "content-type"); rc = SLNSubmissionCreate(pull->session, URI, &sub); if(rc < 0) { alogf("Pull submission error: %s\n", sln_strerror(rc)); goto fail; } rc = SLNSubmissionSetType(sub, type); if(rc < 0) { alogf("Pull submission type error: %s\n", sln_strerror(rc)); goto fail; } for(;;) { if(pull->stop) goto fail; uv_buf_t buf[1] = {}; rc = HTTPConnectionReadBody(*conn, buf); if(rc < 0) { alogf("Pull download error: %s\n", sln_strerror(rc)); goto fail; } if(0 == buf->len) break; rc = SLNSubmissionWrite(sub, (byte_t *)buf->base, buf->len); if(rc < 0) { alogf("Pull write error\n"); goto fail; } } rc = SLNSubmissionEnd(sub); if(rc < 0) { alogf("Pull submission error: %s\n", sln_strerror(rc)); goto fail; } enqueue: HTTPHeadersFree(&headers); async_mutex_lock(pull->mutex); pull->queue[pos] = sub; sub = NULL; pull->filled[pos] = true; async_cond_broadcast(pull->cond); async_mutex_unlock(pull->mutex); return 0; fail: HTTPHeadersFree(&headers); SLNSubmissionFree(&sub); HTTPConnectionFree(conn); return -1; }
static int convert(BlogRef const blog, SLNSessionRef const session, char const *const htmlpath, SLNSubmissionRef *const outmeta, strarg_t const URI, SLNFileInfo const *const src, BlogTypeCheck const types, BlogConverter const converter) { int rc = types(src->type); if(rc < 0) return UV_EINVAL; str_t *tmp = NULL; uv_file html = -1; uv_file file = -1; char const *buf = NULL; SLNSubmissionRef meta = NULL; yajl_gen json = NULL; tmp = SLNRepoCopyTempPath(blog->repo); if(!tmp) rc = UV_ENOMEM; if(rc < 0) goto cleanup; rc = async_fs_open_mkdirp(tmp, O_CREAT | O_EXCL | O_WRONLY, 0400); if(rc < 0) goto cleanup; html = rc; rc = async_fs_open(src->path, O_RDONLY, 0000); if(rc < 0) goto cleanup; file = rc; // We use size+1 to get nul-termination. Kind of a hack. buf = mmap(NULL, src->size+1, PROT_READ, MAP_SHARED, file, 0); if(MAP_FAILED == buf) rc = -errno; if(rc < 0) goto cleanup; if('\0' != buf[src->size]) rc = UV_EIO; // Slightly paranoid. if(rc < 0) goto cleanup; async_fs_close(file); file = -1; if(outmeta) { rc = SLNSubmissionCreate(session, NULL, URI, &meta); if(rc < 0) goto cleanup; rc = SLNSubmissionSetType(meta, SLN_META_TYPE); if(rc < 0) goto cleanup; } SLNSubmissionWrite(meta, (byte_t const *)URI, strlen(URI)); SLNSubmissionWrite(meta, (byte_t const *)STR_LEN("\n\n")); json = yajl_gen_alloc(NULL); if(!json) rc = UV_ENOMEM; if(rc < 0) goto cleanup; yajl_gen_config(json, yajl_gen_print_callback, (void (*)())SLNSubmissionWrite, meta); yajl_gen_config(json, yajl_gen_beautify, (int)true); async_pool_enter(NULL); yajl_gen_map_open(json); rc = converter(html, json, buf, src->size, src->type); yajl_gen_map_close(json); async_pool_leave(NULL); if(rc < 0) goto cleanup; rc = async_fs_fdatasync(html); if(rc < 0) goto cleanup; rc = async_fs_link_mkdirp(tmp, htmlpath); if(rc < 0) goto cleanup; rc = SLNSubmissionEnd(meta); if(rc < 0) goto cleanup; if(outmeta) { *outmeta = meta; meta = NULL; } cleanup: async_fs_unlink(tmp); FREE(&tmp); if(html >= 0) { async_fs_close(html); html = -1; } if(file >= 0) { async_fs_close(file); file = -1; } if(buf) { munmap((void *)buf, src->size+1); buf = NULL; } if(json) { yajl_gen_free(json); json = NULL; } SLNSubmissionFree(&meta); assert(html < 0); assert(file < 0); return rc; }