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; } }
~RequestLogger() { x0::Buffer sstr; sstr << hostname(in_); sstr << " - "; // identity as of identd sstr << username(in_) << ' '; sstr << in_->connection.worker().now().htlog_str().c_str() << " \""; sstr << request_line(in_) << "\" "; sstr << static_cast<int>(in_->status) << ' '; sstr << in_->bytesTransmitted() << ' '; sstr << '"' << getheader(in_, "Referer") << "\" "; sstr << '"' << getheader(in_, "User-Agent") << '"'; sstr << '\n'; if (log_->write(sstr.c_str(), sstr.size()) < static_cast<ssize_t>(sstr.size())) { in_->log(x0::Severity::error, "Could not write to accesslog target. %s", strerror(errno)); } }
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; } }
void onContent(const x0::BufferRef& chunk) { if (chunk.empty()) { if (created_) request_->status = x0::HttpStatus::Created; else request_->status = x0::HttpStatus::NoContent; request_->finish(); ::close(fd_); delete this; } else { ::write(fd_, chunk.data(), chunk.size()); // check return code and possibly early abort request with error code indicating diagnostics } }
~RequestLogger() { x0::Buffer sstr; sstr << hostname(in_); sstr << " - "; // identity as of identd sstr << username(in_) << ' '; sstr << in_->connection.worker().now().htlog_str().c_str() << " \""; sstr << request_line(in_) << "\" "; sstr << static_cast<int>(in_->status) << ' '; sstr << in_->bytesTransmitted() << ' '; sstr << '"' << getheader(in_, "Referer") << "\" "; sstr << '"' << getheader(in_, "User-Agent") << '"'; sstr << '\n'; int rv = ::write(fd_, sstr.data(), sstr.size()); if (rv < 0) { perror("accesslog.write"); } }