示例#1
0
  ////////////////////////////////////////////////////////////////////////
  // componentdefinition
  //
  unsigned Unzip::operator() (tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams& qparams)
  {
    std::string pi = request.getPathInfo();

    log_debug("unzip archive \"" << request.getArg("file") << "\" file \"" << pi << '"');

    try
    {
      unzipFile f(request.getArg("file"));
      unzipFileStream in(f, pi, false);

      // set Content-Type
      std::string contentType = request.getArg("contenttype");
      if (contentType.empty())
        setContentType(request, reply);
      else
        reply.setContentType(contentType);

      reply.out() << in.rdbuf();
    }
    catch (const unzipEndOfListOfFile&)
    {
      log_debug("file \"" << pi << "\" not found in archive");
      return DECLINED;
    }

    return HTTP_OK;
  }
示例#2
0
unsigned PageAsJson::operator() (tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams& qparam)
{
	std::string content = request.getArg("content");
	std::string contentOutput;
	unsigned ret = HTTP_INTERNAL_SERVER_ERROR;

	// handle all pages that mustn't be called by url here
	if(content == "page" ||
		content == "page.json" ||
		content == "404" ||
		content == "500")
	{
		contentOutput = scallComp("404", request);
		ret = HTTP_NOT_FOUND;
	}
	else
	{
		try
		{
			// call content component
			contentOutput = scallComp(content, request, qparam);
			ret = HTTP_OK;
		}
		catch(tnt::NotFoundException&)
		{
			contentOutput = scallComp("404", request);
			ret = HTTP_NOT_FOUND;
		}
		catch(std::runtime_error& e)
		{
			tnt::QueryParams errorParam;
			errorParam.add("errorMessage", e.what());
			contentOutput = scallComp("500", request, errorParam);
			ret = HTTP_INTERNAL_SERVER_ERROR;
		}
	}

	Json::Value replyContent;
	Json::FastWriter jsonWriter;

	replyContent["content"] = contentOutput;
	replyContent["title"] = getPageTitle(request);

	reply.out() << jsonWriter.write(replyContent);

	reply.setContentType("application/json");
	return ret;
}
示例#3
0
  unsigned MbComponent::doCall(tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams& qparam, bool top)
  {
    log_trace("MbComponent " << getCompident());

    tnt::DataChunks data(rawData);

    unsigned url_idx = 0;
    if (urls)
    {
      const char* url = request.getPathInfo().c_str();

      log_debug("search for \"" << url << '"');

      const char** urls_end = urls + data.size();
      const char** it = std::lower_bound(urls, urls_end, url, charpLess);
      if (it == urls_end || std::strcmp(url, *it) != 0)
      {
        log_debug("file \"" << url << "\" not found");
        return DECLINED;
      }

      url_idx = it - urls;

      log_debug("file \"" << url << "\" found; idx=" << url_idx);
    }

    if (top)
    {
      reply.setKeepAliveHeader();
      reply.setContentType(mimetypes[url_idx]);

      std::string maxAgeStr = request.getArg("maxAge");
      unsigned maxAge = maxAgeStr.empty() ? 14400 : cxxtools::convert<unsigned>(maxAgeStr);
      reply.setMaxAgeHeader(maxAge);

      std::string s = request.getHeader(tnt::httpheader::ifModifiedSince);
      if (s == ctimes[url_idx])
        return HTTP_NOT_MODIFIED;

      reply.setHeader(tnt::httpheader::lastModified, ctimes[url_idx]);

      if (request.getEncoding().accept("gzip"))
      {
        cxxtools::ReadLock lock(mutex);
        if (compressedData[url_idx].empty())
        {
          lock.unlock();
          cxxtools::WriteLock wlock(mutex);

          if (compressedData[url_idx].empty())
          {
            // check for compression
            std::string body(data[url_idx].getData(), data[url_idx].getLength());
            log_info("try compress");
            if (reply.tryCompress(body))
            {
              log_info("compressed successfully from " << data[url_idx].getLength() << " to " << body.size());
              compressedData[url_idx] = body;
            }
            else
            {
              log_info("not compressed " << data[url_idx].getLength());
              compressedData[url_idx] = "-";
            }
          }
        }

        if (compressedData[url_idx] != "-")
        {
          log_debug("compressed data found; content size " << data[url_idx].getLength() << " to " << compressedData[url_idx].size());
          reply.setContentLengthHeader(compressedData[url_idx].size());
          reply.setHeader(httpheader::contentEncoding, "gzip");
          if (!request.isMethodHEAD())
          {
            reply.setDirectMode();
            reply.out() << compressedData[url_idx];
          }
          return HTTP_OK;
        }
      }

      reply.setContentLengthHeader(data.size(url_idx));

      if (!request.isMethodHEAD())
      {
        log_debug("send data");
        reply.setDirectMode();
      }
    }

    if (!request.isMethodHEAD())
      reply.out() << data[url_idx];

    return HTTP_OK;
  }