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