<%include>global.ecpp</%include>
<%cpp>

  if (request.getArgs().size() == 0)
    return DECLINED;

  std::string host = request.getHeader(tnt::httpheader::host);
  if (host.empty())
    host = "localhost";

  title.clear();

  char ns = request.getArgDef(0, "A").at(0);

  std::string pathInfo = request.getPathInfo();

  if (!article.good() || article.getUrl() != pathInfo)
  {
    log_info("search article \"" << pathInfo << "\" namespace " << ns);
    article = articleFile.getArticle(ns, pathInfo);
  }
  else
    log_debug("use previous article " << pathInfo);

  if (!article.good())
  {
    log_warn("article " << pathInfo << " not found");
    return DECLINED;
  }

  log_info("article " << pathInfo << " fetched - mime-type "
    << article.getLibraryMimeType() << " redirect " << article.isRedirect());
  log_debug("article index=" << article.getIndex());

  std::string ifModifiedSince = request.getHeader(tnt::httpheader::ifModifiedSince);
  std::string mTime = tnt::HttpMessage::htdate(articleFile.getMTime());
  log_debug("ifModifiedSince=\"" << ifModifiedSince << "\" mTime=\"" << mTime << '"');
  if (!ifModifiedSince.empty() && ifModifiedSince == mTime)
  {
    log_debug("not modified");
    return HTTP_NOT_MODIFIED;
  }
  reply.setHeader(tnt::httpheader::lastModified, mTime);

  if (article.isRedirect())
  {
    article = article.getRedirectArticle();
    log_debug("redirect to " << article.getUrl());
    return reply.redirect(article.getUrl());
  }
  else if (article.getMimeType() != "text/html")
  {
    log_debug("send non-html data");
    callComp("article", request, reply, qparam);
    return HTTP_OK;
  }

  title = article.getTitle();

  int type = request.getPathInfo()[0] == '-' ? typeSpecial
           : article.getNamespace() == 'Q'   ? typeHistory
           : typeArticle;

</%cpp>
<& skin qparam nextComp="article" type=(type) >
