// ---------------------------------------------------------------------------
// - HttpProto.hpp                                                           -
// - afnix:nwg module - http protocol base 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-2009 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_HTTPPROTO_HPP
#define  AFNIX_HTTPPROTO_HPP

#ifndef  AFNIX_PLIST_HPP
#include "Plist.hpp"
#endif

#ifndef  AFNIX_BUFFER_HPP
#include "Buffer.hpp"
#endif

#ifndef  AFNIX_INPUTSTREAM_HPP
#include "InputStream.hpp"
#endif

#ifndef  AFNIX_OUTPUTSTREAM_HPP
#include "OutputStream.hpp"
#endif

namespace afnix {

  /// the HttpProto class is a base class that ease the deployement of the
  /// http protocol. The base class is built with a property list which is
  /// used to define the message header. The class also defines the write
  /// methods which are used to write a message either on an output stream
  /// or into a buffer.
  /// @author amaury darsch

  class HttpProto : public virtual Object {
  public:
    /// map a http status code
    /// @param code the code to map
    static String mapcode (const long code);

  protected:
    /// the protocol version
    String d_vers;
    /// the http header list
    Plist  d_head;

  public:
    /// create a default http protocol object
    HttpProto (void);

    /// reset this protocol object
    virtual void reset (void);

    /// parse an input stream
    /// @param is the input stream to parse
    virtual void parse (InputStream& is) =0;

    /// set the protocol version
    /// @param vers the version to set
    virtual void setvers (const String& vers);

    /// @return the protocol version
    virtual String getvers (void) const;

    /// @return the header length
    virtual long hlength (void) const;
    
    /// @return true if a header property exists
    virtual bool hexists (const String& name) const;
    
    /// set a header property by name and litteral
    /// @param name the property name
    /// @param lval the property value
    virtual void hset (const String& name, const Literal& pval);

    /// @return a header property by index
    virtual Property* hget (const long index) const;

    /// @return a header property by name
    virtual Property* hfind (const String& name) const;

    /// @return a header property by name or throw an exception
    virtual Property* hlookup (const String& name) const;

    /// @return a header property value by name
    virtual String hmap (const String& name) const;

    /// parse a header from an input stream 
    /// @param is the input stream to parse
    virtual void hparse (InputStream& is);

    /// write the http header to an output stream
    /// @param os the output stream
    virtual void write (OutputStream& os) const;

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

  public:
    /// @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
