xref: /haiku/src/add-ons/print/transports/ipp/HttpURLConnection.h (revision 302f62604763c95777d6d04cca456e876f471c4f)
1 // Sun, 18 Jun 2000
2 // Y.Takagi
3 
4 #ifndef __HttpURLConnection_H
5 #define __HttpURLConnection_H
6 
7 #include <istream.h>
8 #include <ostream.h>
9 
10 #include <list>
11 #include <string>
12 
13 #include "URL.h"
14 
15 using namespace std;
16 
17 class Socket;
18 
19 enum HTTP_RESPONSECODE {
20 	HTTP_UNKNOWN		= -1,	//
21 
22 	HTTP_CONTINUE		= 100,	// Everything OK, keep going...
23 	HTTP_SWITCH_PROC	= 101,	// Switching Protocols
24 
25 	HTTP_OK				= 200,	// OPTIONS/GET/HEAD/POST/TRACE command was successful
26 	HTTP_CREATED,				// PUT command was successful
27 	HTTP_ACCEPTED,				// DELETE command was successful
28 	HTTP_NOT_AUTHORITATIVE,		// Information isn't authoritative
29 	HTTP_NO_CONTENT,			// Successful command, no new data
30 	HTTP_RESET,					// Content was reset/recreated
31 	HTTP_PARTIAL,				// Only a partial file was recieved/sent
32 
33 	HTTP_MULTI_CHOICE	= 300,	// Multiple files match request
34 	HTTP_MOVED_PERM,			// Document has moved permanently
35 	HTTP_MOVED_TEMP,			// Document has moved temporarily
36 	HTTP_SEE_OTHER,				// See this other link...
37 	HTTP_NOT_MODIFIED,			// File not modified
38 	HTTP_USE_PROXY,				// Must use a proxy to access this URI
39 
40 	HTTP_BAD_REQUEST	= 400,	// Bad request
41 	HTTP_UNAUTHORIZED,			// Unauthorized to access host
42 	HTTP_PAYMENT_REQUIRED,		// Payment required
43 	HTTP_FORBIDDEN,				// Forbidden to access this URI
44 	HTTP_NOT_FOUND,				// URI was not found
45 	HTTP_BAD_METHOD,			// Method is not allowed
46 	HTTP_NOT_ACCEPTABLE,		// Not Acceptable
47 	HTTP_PROXY_AUTH,			// Proxy Authentication is Required
48 	HTTP_REQUEST_TIMEOUT,		// Request timed out
49 	HTTP_CONFLICT,				// Request is self-conflicting
50 	HTTP_GONE,					// Server has gone away
51 	HTTP_LENGTH_REQUIRED,		// A content length or encoding is required
52 	HTTP_PRECON_FAILED,			// Precondition failed
53 	HTTP_ENTITY_TOO_LARGE,		// URI too long
54 	HTTP_REQ_TOO_LONG,			// Request entity too large
55 	HTTP_UNSUPPORTED_TYPE,		// The requested media type is unsupported
56 
57 	HTTP_SERVER_ERROR	= 500,	// Internal server error
58 	HTTP_INTERNAL_ERROR,		// Feature not implemented
59 	HTTP_BAD_GATEWAY,			// Bad gateway
60 	HTTP_UNAVAILABLE,			// Service is unavailable
61 	HTTP_GATEWAY_TIMEOUT,		// Gateway connection timed out
62 	HTTP_VERSION				// HTTP version not supported
63 };
64 
65 struct Field {
66 	string key;
67 	string value;
68 	Field() {}
69 	Field(char *field);
70 	Field(const char *k, const char *v);
71 	Field(const Field &);
72 	Field &operator = (const Field &);
73 	bool operator == (const Field &);
74 };
75 
76 typedef list<Field>	Fields;
77 
78 class HttpURLConnection {
79 public:
80 	HttpURLConnection(const URL &url);
81 	virtual ~HttpURLConnection();
82 
83 	virtual void connect();
84 	void disconnect();
85 
86 	void setRequestMethod(const char *method);
87 	const char *getRequestMethod() const;
88 	void setRequestProperty(const char *key, const char *value);
89 
90 	const char *getContentType();
91 	const char *getContentEncoding();
92 	int getContentLength();
93 	long getDate();
94 	const char *getHeaderField(int n);
95 	const char *getHeaderField(const char *);
96 	const URL &getURL() const;
97 	HTTP_RESPONSECODE getResponseCode();
98 	const char *getResponseMessage();
99 
100 	bool getDoInput() const;
101 	bool getDoOutput() const;
102 	void setDoInput(bool doinput);
103 	void setDoOutput(bool dooutput);
104 
105 	istream &getInputStream();
106 	ostream &getOutputStream();
107 
108 	const char *getLastError() const;
109 	void setLastError(const char *);
110 
111 protected:
112 	bool connected;
113 	bool doInput;
114 	bool doOutput;
115 	URL  url;
116 
117 	virtual void action();
118 	virtual void setRequest();
119 	virtual void setContent();
120 	virtual void getResponse();
121 	virtual void getContent();
122 
123 private:
124 	Fields *__request;
125 	Fields *__response;
126 	Socket *__sock;
127 	string __method;
128 	string __response_message;
129 	HTTP_RESPONSECODE __response_code;
130 	string __error_msg;
131 };
132 
133 inline const char *HttpURLConnection::getLastError() const
134 {
135 	return __error_msg.c_str();
136 }
137 
138 inline void HttpURLConnection::setLastError(const char *e)
139 {
140 	__error_msg = e;
141 }
142 
143 #endif	// __HttpURLConnection_H
144