// ---------------------------------------------------------------------------
// - HttpReply.hpp                                                           -
// - afnix:nwg module - http reply class definition                          -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  is  distributed in  the hope  that it will be useful, but -
// - without  any  warranty;  without  even   the   implied    warranty   of -
// - merchantability or fitness for a particular purpose.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2008 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_HTTPREPLY_HPP
#define  AFNIX_HTTPREPLY_HPP

#ifndef  AFNIX_MIME_HPP
#include "Mime.hpp"
#endif

#ifndef  AFNIX_COOKIE_HPP
#include "Cookie.hpp"
#endif

#ifndef  AFNIX_HTTPPROTO_HPP
#include "HttpProto.hpp"
#endif

namespace afnix {

  /// The HttpReply class is a simple designed to format a http reply.
  /// When the write method is called, the header and the buffer are 
  /// formatted to the output stream argument. The class creates initially
  /// the 'content-type' property which is initialized to the 'text/plain'
  /// mime value. The object can also be filled with a mime document. This
  /// kind of reply is usefull when responding to cgi request. In that case,
  /// the content-type is changed according to the mime document type.
  /// @author amaury darsch

  class HttpReply : public HttpProto {
  protected:
    /// the http buffer
    Buffer d_hbuf;

  public:
    /// create an empty http reply
    HttpReply (void);

    /// create an empty reply with a content type
    /// @param type the content type to use
    HttpReply (const String& type);

    /// @return the class name
    String repr (void) const;

    /// set a cookie in the header
    /// @param cook the cookie to set
    virtual void setcook (const Cookie& cook);

    /// add a string to the http buffer
    /// @param lval the literal value to add
    virtual void addhbuf (const Literal& lval);

    /// add a buffer content to the http buffer
    /// @param buf the buffer to add
    virtual void addhbuf (const Buffer& buf);

    /// add a mime document to the http buffer
    /// @param mime the document to add
    virtual void addhbuf (const Mime& mime);

    /// set the hhtp reply status code
    /// @param code the http status code
    virtual void setstatus (const long code);

    /// reply a redirect status with a url
    /// @param url the target url
    virtual void redirect (const String& url);

    /// write the http reply to an output stream
    /// @param os the output stream
    void write (Output& os) const;

    /// write the http reply to a buffer
    /// @param buf the target buffer
    void write (Buffer& buf) const;

  private:
    // make the copy constructor private
    HttpReply (const HttpReply&);
    // make the assignment operator private
    HttpReply& operator = (const HttpReply&);

  public:
    /// create a new object in a generic object
    /// @param argv the argument vector
    static Object* mknew (Vector* argv);

    /// @return true if the given quark is defined
    bool isquark (const long quark, const bool hflg) const;

    /// apply this object with a set of arguments and a quark
    /// @param robj  the current runnable
    /// @param nset  the current nameset    
    /// @param quark the quark to apply these arguments
    /// @param argv  the arguments to apply
    Object* apply (Runnable* robj, Nameset* nset, const long quark,
                   Vector* argv);
  };
}

#endif
