-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGoogleCal.cpp
More file actions
95 lines (79 loc) · 1.68 KB
/
Copy pathGoogleCal.cpp
File metadata and controls
95 lines (79 loc) · 1.68 KB
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
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
// Import my own headers.
#include "common.h"
#include "Debug.h"
WiFiClientSecure client;
String WebFetch(String url)
{
const char *strURL;
String Response;
char server[80];
bool Redirect = 0;
strURL = url.c_str();
GCAL_PRINT("GCAL:URL:"); GCAL_PRINTLN(strURL);
if (memcmp("https://", strURL, 8) == 0)
{
int i;
for(i=0; i<80; i++)
{
if (strURL[i+8] == '/')
break;
server[i] = strURL[i+8];
}
server[i] = 0;
}
GCAL_PRINT("GCAL:server:"); GCAL_PRINTLN(server);
if (!client.connect(server, 443))
GCAL_PRINTLN("GCAL:No connection");
else
{
GCAL_PRINTLN("GCAL:Connect");
// Make a HTTP request:
client.print("GET ");
client.print(url);
client.println(" HTTP/1.0");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
String header;
while (client.connected())
{
String line = client.readStringUntil('\n');
header = header + line + "\n";
if (line.startsWith("Location: "))
{
Redirect = 1;
Response = line.substring(line.indexOf("http"));
GCAL_PRINT("GCAL:REDIRECT:"); GCAL_PRINTLN(Response);
}
if (line == "\r")
break;
}
GCAL_PRINTLN("GCAL:HEADER:"); GCAL_PRINTLN(header);
String body;
while (client.available())
{
String line = client.readStringUntil('\n');
body = body + line + "\n";
if (line == "\r")
break;
}
if (!Redirect)
{
GCAL_PRINTLN("GCAL:BODY:"); GCAL_PRINTLN(body);
Response = body;
}
client.stop();
}
return(Response);
}
String FetchGCal(String url)
{
String Return1;
String Return2;
Return1 = WebFetch(url);
Return2 = WebFetch(Return1);
return(Return2);
}