xref: /haiku/src/add-ons/print/transports/ipp/IppURLConnection.cpp (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
1 // Sun, 18 Jun 2000
2 // Y.Takagi
3 
4 #ifdef WIN32
5 #include <winsock.h>
6 #include <ostream>
7 #include <cstring>
8 #else
9 #include <net/socket.h>
10 #include <ostream.h>
11 #include <string.h>
12 #include <stdio.h>
13 char *itoa(int i, char *buf, int unit)
14 {
15 	sprintf(buf, "%d", i);
16 	return buf;
17 }
18 #define stricmp		strcasecmp
19 #define strnicmp	strncasecmp
20 #endif	// WIN32
21 
22 #include <list>
23 #include "IppURLConnection.h"
24 #include "IppContent.h"
25 
26 IppURLConnection::IppURLConnection(const URL &Url)
27 	: HttpURLConnection(Url)
28 {
29 	__ippRequest  = NULL;
30 	__ippResponse = new IppContent;
31 	setRequestMethod("POST");
32 	setRequestProperty("Content-Type", "application/ipp");
33 	setRequestProperty("Cache-Control", "no-cache");
34 	setRequestProperty("Pragma", "no-cache");
35 }
36 
37 IppURLConnection::~IppURLConnection()
38 {
39 	if (__ippRequest) {
40 		delete __ippRequest;
41 	}
42 
43 	if (__ippResponse) {
44 		delete __ippResponse;
45 	}
46 }
47 
48 void IppURLConnection::setIppRequest(IppContent *obj)
49 {
50 	if (__ippRequest) {
51 		delete __ippRequest;
52 	}
53 	__ippRequest = obj;
54 }
55 
56 
57 const IppContent *IppURLConnection::getIppResponse() const
58 {
59 	return __ippResponse;
60 }
61 
62 void IppURLConnection::setRequest()
63 {
64 	if (connected) {
65 		char buf[64];
66 		itoa(__ippRequest->length(), buf, 10);
67 		setRequestProperty("Content-Length", buf);
68 		HttpURLConnection::setRequest();
69 	}
70 }
71 
72 void IppURLConnection::setContent()
73 {
74 	if (connected && __ippRequest) {
75 		ostream &os = getOutputStream();
76 		os << *__ippRequest;
77 	}
78 }
79 
80 inline bool is_contenttype_ipp(const char *s)
81 {
82 	return strnicmp(s, "application/ipp", 15) ? false : true;
83 }
84 
85 void IppURLConnection::getContent()
86 {
87 	if (connected) {
88 		if (getResponseCode() == HTTP_OK && is_contenttype_ipp(getContentType())) {
89 			istream &is = getInputStream();
90 			is >> *__ippResponse;
91 		} else {
92 			HttpURLConnection::getContent();
93 		}
94 	}
95 }
96 
97 ostream &IppURLConnection::printIppRequest(ostream &os)
98 {
99 	return __ippRequest->print(os);
100 }
101 
102 ostream &IppURLConnection::printIppResponse(ostream &os)
103 {
104 	if (getResponseCode() == HTTP_OK && is_contenttype_ipp(getContentType())) {
105 		return __ippResponse->print(os);
106 	}
107 	return os;
108 }
109