-
Notifications
You must be signed in to change notification settings - Fork 24
/
HttpResponse.cpp
187 lines (152 loc) · 3.79 KB
/
HttpResponse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Project: 1Sheeld Library
File: HttpResponse.cpp
Version: 1.2
Compiler: Arduino avr-gcc 4.3.2
Author: Integreight
Date: 2015.1
*/
#define FROM_ONESHEELD_LIBRARY
#include "OneSheeld.h"
#include "InternetShield.h"
HttpResponse::HttpResponse()
{
isInit=false;
isDisposedTriggered=false;
statusCode=0;
totalBytesCount=0;
bytes=NULL;
bytesCount = 0;
requestId = 0;
index =0;
callbacksRequested = 0;
}
int HttpResponse::getStatusCode()
{
return statusCode;
}
int HttpResponse::getBytesCount()
{
return bytesCount;
}
char * HttpResponse::getBytes()
{
return bytes;
}
unsigned long HttpResponse::getTotalBytesCount()
{
return totalBytesCount;
}
unsigned long HttpResponse::getCurrentIndex()
{
return index;
}
void HttpResponse::getTheseBytes(unsigned long start,int size)
{
if(isInit)
{
index=start;
byte startArray[4] ;
startArray[0] = start & 0xFF;
startArray[1] = (start >> 8) & 0xFF;
startArray[2] = (start >> 16) & 0xFF;
startArray[3] = (start >> 24) & 0xFF;
byte sizeArray[2] ;
sizeArray[1] = (size >> 8) & 0xFF;
sizeArray[0] = size & 0xFF;
byte reqId[2] ;
reqId[1] = (requestId >> 8) & 0xFF;
reqId[0] = requestId & 0xFF;
OneSheeld.sendShieldFrame(INTERNET_ID,0,RESPONSE_GET_NEXT_BYTES,3,new FunctionArg(2,reqId),
new FunctionArg(4,startArray),
new FunctionArg(2,sizeArray));
}
}
void HttpResponse::getNextBytes(int size)
{
getTheseBytes(index,size);
}
void HttpResponse::setOnNextResponseBytesUpdate(void (*userFunction)(HttpResponse &))
{
callbacksRequested |= RESPONSE_GET_NEXT_RESPONSE_BIT;
getNextCallBack = userFunction;
}
void HttpResponse::setOnError(void (*userFunction)(int errorNumber))
{
callbacksRequested |= RESPONSE_GET_ERROR_BIT;
getErrorCallBack = userFunction;
}
void HttpResponse::setOnJsonResponse(void (*userFunction)(JsonKeyChain & chain,char data[]))
{
callbacksRequested |= RESPONSE_GET_JSON_BIT;
getJsonCallBack = userFunction;
}
void HttpResponse::setOnJsonArrayLengthResponse(void (*userFunction)(JsonKeyChain & chain,unsigned long size))
{
callbacksRequested |= RESPONSE_GET_JSON_ARRAY_LENGTH_BIT;
getJsonArrayLengthCallBack = userFunction;
}
bool HttpResponse::isSentFully()
{
return (index>=totalBytesCount);
}
void HttpResponse::dispose(bool sendFrame)
{
isDisposedTriggered = true;
if(isInit && bytesCount!=0 && bytes!=NULL)
{
free(bytes);
}
isInit=false;
bytes=NULL;
index = 0;
bytesCount = 0;
statusCode = 0;
totalBytesCount = 0;
byte reqId[2] ;
reqId[1] = (requestId >> 8) & 0xFF;
reqId[0] = requestId & 0xFF;
if(sendFrame)
{
OneSheeld.sendShieldFrame(INTERNET_ID,0,RESPONSE_DISPOSE,1,new FunctionArg(2,reqId));
callbacksRequested=0;
}
}
bool HttpResponse::isDisposed()
{
return isDisposedTriggered;
}
void HttpResponse::resetIndex(unsigned long x)
{
index=x;
}
void HttpResponse::getHeader(const char * headerName , void (*userFunction)(char incomingheaderName [],char IncomingHeaderValue[]))
{
if(isInit)
{
//Check length of string
int headerNameLength = strlen(headerName);
if(!headerNameLength) return;
callbacksRequested |= RESPONSE_INPUT_GET_HEADER_BIT;
getHeaderCallBack = userFunction;
byte reqId[2] ;
reqId[1] = (requestId >> 8) & 0xFF;
reqId[0] = requestId & 0xFF;
OneSheeld.sendShieldFrame(INTERNET_ID,0,RESPONSE_GET_HEADER,2,new FunctionArg(2,reqId),new FunctionArg(headerNameLength,(byte *)headerName));
}
}
JsonKeyChain HttpResponse::operator[](int key)
{
JsonKeyChain chain(requestId);
return chain[key];
}
JsonKeyChain HttpResponse::operator[](const char *key)
{
if(!strlen(key)) return 0;
JsonKeyChain chain(requestId);
return chain[key];
}
HttpResponse::~HttpResponse()
{
if(isInit && bytesCount!=0)free(bytes);
}