bool execute() { TRACE("Put.file: %s\n", request_->fileinfo->path().c_str()); if (request_->contentAvailable()) { created_ = !request_->fileinfo->exists(); if (!created_) ::unlink(request_->fileinfo->path().c_str()); fd_ = ::open(request_->fileinfo->path().c_str(), O_WRONLY | O_CREAT, 0666); if (fd_ < 0) { perror("WebDav.Put(open)"); request_->status = x0::HttpStatus::Forbidden; request_->finish(); delete this; return true; } request_->setBodyCallback<Put, &Put::onContent>(this); } else { request_->status = x0::HttpStatus::NotImplemented; request_->finish(); delete this; } return true; }
// Handler, invoked when a content chunk has been fully written to the client // (or an error occurred). // // We will try to read another input chunk to echo back to the client, // or just finish the response if failed. void contentWritten() { // TODO (write-)error handling; pass errno to this fn, too. // is there more data available? if (!request_->contentAvailable()) { // could not read another input chunk, so finish processing this request. request_->finish(); delete this; } }
void run() { // set response status code request_->status = x0::HttpError::Ok; // set response header "Content-Length", // if request content were not encoded // and if we've received its request header "Content-Length" if (!request_->requestHeader("Content-Encoding")) if (x0::BufferRef value = request_->requestHeader("Content-Length")) request_->responseHeaders.overwrite("Content-Length", value.str()); // try to read content (if available) and pass it on to our onContent handler, // or fall back to just write HELLO (if no request content body was sent). if (request_->contentAvailable()) { request_->setBodyCallback<EchoHandler, &EchoHandler::onContent>(this); } else { request_->write<x0::BufferSource>("I'm an HTTP echo-server, dude.\n"); request_->finish(); delete this; } }