static void wurfld_input_handler(iomux_t *iomux, int fd, void *data, int len, void *priv) { wurfld_connection_context *ctx = (wurfld_connection_context *)priv; if (!ctx) return; DEBUG1("New data on fd %d", fd); fbuf_add_binary(ctx->input, data, len); if (fbuf_used(ctx->input) < 4) return; // check if we have a complete requset char *current_data = fbuf_end(ctx->input) - (use_http ? 4 : 1); char *request_terminator = use_http ? strstr(current_data, "\r\n\r\n") : strstr(current_data, "\n"); if (!request_terminator && use_http) { // support some broken clients/requests request_terminator = strstr(current_data, "\n\n"); } if (request_terminator) { // we have a complete request so we can now start // background worker to handle it pthread_t worker_thread; ctx->fd = fd; // let the worker take care of the fd from now on iomux_remove(iomux, fd); if (single_thread) { worker(ctx); } else { pthread_create(&worker_thread, NULL, worker, ctx); pthread_detach(worker_thread); } } }
int fbuf_add(fbuf_t *fbuf, const char *data) { int datalen; if (!data || data[0] == '\0') // nothing to be done return 0; datalen = strlen(data); return fbuf_add_binary(fbuf, data, datalen); }
static char *unescape_uri_request(char *uri) { fbuf_t buf = FBUF_STATIC_INITIALIZER; char *p = uri; while (*p != 0) { char *n = p; while (*n != '%' && *n != 0) n++; fbuf_add_binary(&buf, p, n-p); p = n; if (*n != 0) { // p and n now both point to % p+=3; n++; int c; if (sscanf(n, "%02x", &c) == 1) fbuf_add_binary(&buf, (char *)&c, 1); else WARN("Can't unescape uri byte"); } } char *data = fbuf_data(&buf); return data; }
fbuf_t * fbuf_duplicate(fbuf_t *fbufsrc) { fbuf_t *fbufdst = fbuf_create(fbufsrc->maxlen); if (!fbufdst) return NULL; if (fbufsrc->used) fbuf_add_binary(fbufdst, fbufsrc->data + fbufsrc->skip, fbufsrc->used); DEBUG_FBUF_INFO(fbufsrc, "original"); DEBUG_FBUF_INFO(fbufdst, "duplicate"); return fbufdst; }