Blame Response.h

Packit a4aae4
Packit a4aae4
// -*- mode: c++; c-basic-offset:4 -*-
Packit a4aae4
Packit a4aae4
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
Packit a4aae4
// Access Protocol.
Packit a4aae4
Packit a4aae4
// Copyright (c) 2002,2003 OPeNDAP, Inc.
Packit a4aae4
// Author: James Gallagher <jgallagher@opendap.org>
Packit a4aae4
//
Packit a4aae4
// This library is free software; you can redistribute it and/or
Packit a4aae4
// modify it under the terms of the GNU Lesser General Public
Packit a4aae4
// License as published by the Free Software Foundation; either
Packit a4aae4
// version 2.1 of the License, or (at your option) any later version.
Packit a4aae4
//
Packit a4aae4
// This library is distributed in the hope that it will be useful,
Packit a4aae4
// but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4aae4
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4aae4
// Lesser General Public License for more details.
Packit a4aae4
//
Packit a4aae4
// You should have received a copy of the GNU Lesser General Public
Packit a4aae4
// License along with this library; if not, write to the Free Software
Packit a4aae4
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit a4aae4
//
Packit a4aae4
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
Packit a4aae4
Packit a4aae4
#ifndef response_h
Packit a4aae4
#define response_h
Packit a4aae4
Packit a4aae4
#include <cstdio>
Packit a4aae4
#include <string>
Packit a4aae4
//#include <iostream>
Packit a4aae4
#include <fstream>
Packit a4aae4
Packit a4aae4
#include "ObjectType.h"
Packit a4aae4
#include "debug.h"
Packit a4aae4
Packit a4aae4
namespace libdap
Packit a4aae4
{
Packit a4aae4
Packit a4aae4
/** Encapsulate a response. Instead of directly returning the FILE pointer
Packit a4aae4
    from which a response is read, return an instance of this object. For a
Packit a4aae4
    simple system where all that needs to be done to free the stream and its
Packit a4aae4
    associated resources, this is overkill. However, some streams may require
Packit a4aae4
    complex operations to free their resources once the client is done with
Packit a4aae4
    the stream. Those classes should return a subclass of Response
Packit a4aae4
    which has those operations built into the destructor.
Packit a4aae4
Packit a4aae4
    @todo If the code that parses the MIME headers was moved from Connect and
Packit a4aae4
    HTTPConnect to this class and its children, it would be easier to build
Packit a4aae4
    a FileConnect class (or maybe the specifics of the connection type could
Packit a4aae4
    be held in the Response object and HTTPConnect and the to-be-written
Packit a4aae4
    FileConnect would not be needed). */
Packit a4aae4
class Response
Packit a4aae4
{
Packit a4aae4
private:
Packit a4aae4
    /// The data stream
Packit a4aae4
    FILE *d_stream;
Packit a4aae4
    std::fstream *d_cpp_stream;
Packit a4aae4
Packit a4aae4
    /// Response object type
Packit a4aae4
    ObjectType d_type;
Packit a4aae4
    /// Server version
Packit a4aae4
    std::string d_version;
Packit a4aae4
    /// The DAP server's protocol
Packit a4aae4
    std::string d_protocol;
Packit a4aae4
    /// The HTTP response code
Packit a4aae4
    int d_status;
Packit a4aae4
Packit a4aae4
protected:
Packit a4aae4
    /** @name Suppressed default methods */
Packit a4aae4
    //@{
Packit a4aae4
    Response(const Response &);
Packit a4aae4
    Response &operator=(const Response &);
Packit a4aae4
    //@}
Packit a4aae4
Packit a4aae4
public:
Packit a4aae4
    Response() : d_stream(0), d_cpp_stream(0), d_type(unknown_type),  d_version("dods/0.0"), d_protocol("2.0"),
Packit a4aae4
		d_status(0)
Packit a4aae4
	{ }
Packit a4aae4
Packit a4aae4
    /** Initialize with a stream. Create an instance initialized to a stream.
Packit a4aae4
	by default get_type() and get_version() return default values of
Packit a4aae4
	unknown_type and "dods/0.0", respectively. Specializations (see
Packit a4aae4
	HTTPResponse and HTTPConnect) may fill these fields in with other
Packit a4aae4
	values.
Packit a4aae4
        @param s Read data from this stream.
Packit a4aae4
        @param status The HTTP response status code.*/
Packit a4aae4
    Response(FILE *s, int status = 0) : d_stream(s), d_cpp_stream(0), d_type(unknown_type),
Packit a4aae4
            d_version("dods/0.0"), d_protocol("2.0"), d_status(status) { }
Packit a4aae4
Packit a4aae4
    Response(std::fstream *s, int status = 0) : d_stream(0), d_cpp_stream(s), d_type(unknown_type),
Packit a4aae4
            d_version("dods/0.0"), d_protocol("2.0"), d_status(status) { }
Packit a4aae4
Packit a4aae4
    /** Close the stream. */
Packit a4aae4
    virtual ~Response()
Packit a4aae4
    {
Packit a4aae4
        if (d_stream)
Packit a4aae4
            fclose(d_stream);
Packit a4aae4
        if (d_cpp_stream)
Packit a4aae4
        	d_cpp_stream->close();
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    /** @name getters */
Packit a4aae4
    //@{
Packit a4aae4
    virtual int get_status() const {  return d_status; }
Packit a4aae4
    virtual FILE *get_stream() const { return d_stream; }
Packit a4aae4
    virtual std::istream *get_cpp_stream() const { return d_cpp_stream; }
Packit a4aae4
Packit a4aae4
    virtual ObjectType get_type() const { return d_type; }
Packit a4aae4
    virtual std::string get_version() const { return d_version; }
Packit a4aae4
    virtual std::string get_protocol() const { return d_protocol; }
Packit a4aae4
    //@}
Packit a4aae4
Packit a4aae4
    /** @name setters */
Packit a4aae4
    //@{
Packit a4aae4
    virtual void set_status(int s) { d_status = s; }
Packit a4aae4
Packit a4aae4
    virtual void set_stream(FILE *s) { d_stream = s; }
Packit a4aae4
    virtual void set_cpp_stream(std::istream *s) { d_cpp_stream = dynamic_cast<std::fstream*>(s); }
Packit a4aae4
Packit a4aae4
    virtual void set_type(ObjectType o) { d_type = o; }
Packit a4aae4
    virtual void set_version(const std::string &v) { d_version = v; }
Packit a4aae4
    virtual void set_protocol(const std::string &p) { d_protocol = p; }
Packit a4aae4
    //@}
Packit a4aae4
};
Packit a4aae4
Packit a4aae4
} // namespace libdap
Packit a4aae4
Packit a4aae4
#endif // response_h