Ejemplo n.º 1
0
  ////////////////////////////////////////////////////////////////////////
  // component definition
  //
  unsigned Setheader::operator() (tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams&)
  {
    for (tnt::HttpRequest::args_type::const_iterator it = request.getArgs().begin();
        it != request.getArgs().end(); ++it)
      reply.setHeader(it->first + ':' , it->second);

    return DECLINED;
  }
Ejemplo n.º 2
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;
  }