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>
12char *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
24IppURLConnection::IppURLConnection(const URL &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
35IppURLConnection::~IppURLConnection()
36{
37	if (__ippRequest) {
38		delete __ippRequest;
39	}
40
41	if (__ippResponse) {
42		delete __ippResponse;
43	}
44}
45
46void IppURLConnection::setIppRequest(IppContent *obj)
47{
48	if (__ippRequest) {
49		delete __ippRequest;
50	}
51	__ippRequest = obj;
52}
53
54
55const IppContent *IppURLConnection::getIppResponse() const
56{
57	return __ippResponse;
58}
59
60void 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
70void IppURLConnection::setContent()
71{
72	if (connected && __ippRequest) {
73		ostream &os = getOutputStream();
74		os << *__ippRequest;
75	}
76}
77
78inline bool is_contenttype_ipp(const char *s)
79{
80	return strnicmp(s, "application/ipp", 15) ? false : true;
81}
82
83void 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
95ostream &IppURLConnection::printIppRequest(ostream &os)
96{
97	return __ippRequest->print(os);
98}
99
100ostream &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