-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctional WeatherAPI
More file actions
237 lines (183 loc) · 7.47 KB
/
Functional WeatherAPI
File metadata and controls
237 lines (183 loc) · 7.47 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* adder.c - a minimal CGI program that subtracts two numbers
*/
/* $begin adder */
#include "../csapp.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* int open_listenfd(char *port) {
strcut addrinfo hints, *listp, *p;
int listenfd, optval=1;
} */
/*
int Open_clientfd(char *hostname, char *port){
int clientfd;
struct addrinfo hints, *listp, *p;
/* Get a list of potential server addresses
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_socktype = SOCK_STREAM; /* Open a connenction
hints.ai_flags = AI_NUMERICSERV; /* using numeric port arg
hints.ai_flags != AI_ADDRCONFIG; /* Recommended for connection
Getaddrinfo(hostname, port, &hints, &listp);
}
*/
#if 0
//echo server:
void echo(int connfd)
{
size_t n;
char buf[MAXLINE];
rio_t rio;
Rio_readinitb(&rio, connfd);
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { //line:netp:echo:eof
printf("server received %d bytes, and the string is %s\n", (int)n, buf);
Rio_writen(connfd, buf, n);
//printf("server received %s string\n", buf);
}
}
void echo(int connfd);
//echoserver:
int main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t clientlen;
struct sockaddr_storage clientaddr; /* Enough space for any address */ //line:netp:echos\
erveri:sockaddrstorage
char client_hostname[MAXLINE], client_port[MAXLINE];
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
listenfd = Open_listenfd(argv[1]);
while (1) { //infinite loop, classic c construct
clientlen = sizeof(struct sockaddr_storage);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
Getnameinfo((SA *) &clientaddr, clientlen, client_hostname, MAXLINE,
client_port, MAXLINE, 0); //try to find out where it's coming from
printf("Connected to (%s, %s)\n", client_hostname, client_port);
//echo(connfd);
Close(connfd);
}
#endif
// }
#if 0
// THIS IS ECHO SERVER
int listenfd, connfd;
socklen_t clientlen;
struct sockaddr_storage clientaddr; /* Enough space for any address */ //line:netp:echos\
erveri:sockaddrstorage
char client_hostname[MAXLINE], client_port[MAXLINE];
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
listenfd = Open_listenfd(argv[1]);
while (1) { //infinite loop, classic c construct
clientlen = sizeof(struct sockaddr_storage);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
Getnameinfo((SA *) &clientaddr, clientlen, client_hostname, MAXLINE,
client_port, MAXLINE, 0); //try to find out where it's coming from
printf("Connected to (%s, %s)\n", client_hostname, client_port);
//echo(connfd);
Close(connfd);
}
#endif
int main(int argc, char **argv){
char *zipcode, *urlBegin, *urlEnd, *s;
char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE];
int n1=0, n2=0;
/* Extract the zipCode */
zipcode = strncat(getenv("QUERY_STRING"), "\0", 5);
//urlBegin ="/data/2.5/weather?zip=";
//urlEnd = ",us&units=imperial&APPID=364ebbea13d49a5e8e505870368c78be";
//s = strcat(urlBegin, zipcode);
// s = strcat(s, urlEnd);
//sprintf(content, "%s \n<p>", s);
char buffer[MAXLINE];
char buffer2[MAXLINE];
// THIS IS WHERE ECHOclient COMES IN
int clientfd, n;
sprintf(buffer, "GET /data/2.5/weather?zip=19004,us&APPID=364ebbea13d49a5e8e505870368c78be\n");
char *host, *port, buf[MAXLINE];
rio_t rio;
host = "162.243.53.59";
//"192.241.169.168"
//http://www.openweathermap.org";
//data/2.5/weather?zip=19004,us&APPID=364ebbea13d49a5e8e505870368c78be";
port = "80";
fprintf(stderr, "about to do clientfd\n");
clientfd = Open_clientfd(host, port);
Rio_readinitb(&rio,clientfd);
if (clientfd == -1) {
fprintf(stderr, "ERROR: connection could not be established.");
}
char response[MAXLINE];
Rio_writen(clientfd, buffer, MAXLINE);
Rio_readlineb(&rio, buffer2, MAXLINE);
Close(clientfd);
fprintf(stderr, "%s", buffer2);
fprintf(stderr, "made it to the JSON output");
char *response_split; // split response on 1st occurence
response_split = strchr(buffer2, '[');
*response_split = '\0';
//strcpy(response, response_split +1);
//fprintf(stderr, "made it passed client fd\n");
//fprintf(stderr, "register with rio is %d \n", rio);
//Rio_readnb(&rio, buf, MAXLINE);
//fprintf(stderr, "this is buf: %s", buf);
//while((n=Rio_readlineb(&rio,buf,MAXLINE))!=0){
// fprintf(stderr, "going through the while loop");
//Rio_writen(STDOUT_FILENO, buf, n);
//}
// while (Fgets(buf,MAXLINE, stdin) != NULL){
//Rio_writen(clientfd, buf, strlen(buf)); //Send line to server
//Rio_readlineb(&rio, buf, MAXLINE); // recieve line from server
//printf("echo:");
//Fputs(buf, stdout);
//printf("type:"); fflush(stdout);
//fprintf(stderr, "about to do command 1 and 2");
//fprintf(stderr, "about to run GET request\n \n");
const char *command1 = "telnet api.openweathermap.org 80";
const char *command = "GET /data/2.5/weather?zip=19004,us&APPID=364ebbea13d49a5e8e505870368c78be";
//int result = 0;
//int system(command1);
//int system(command);
//fprintf(stderr, "now trying command3\n ");
//char command3[50];
//strcpy(command3,"ls -l");
//system(command);
//return(0);
//fprintf(stderr, "GET request just ran \n");
//fprintf(stderr, "this is what happened: %d", result);
/* Argument list starts with "?"
Arguments separated by "&"
Spaces represented by "+" or "%20"
Example:
argument list starts with "https://newsapi.org/v2/everything?" +
"q=Apple&" +
"from=2018-12-17&" +
"sortBy=popularity&" +
"apiKey=f8f3dee3f2524f47a1309bc2381b8cb2"; */
/* Make the response body */
//sprintf(content, "%s this is result");
//sprintf(content, "%sWelcome to Zoe & Mori's News API: ");
//sprintf(content, "%sZoe & Mori's News portal.\r\n<p>", content);
//replace next line with how to parse through JSON given and return it nicely
//sprintf(content, "%sThe answer is: %d \r\n<p>",
//content, n1, n1);
sprintf(content,"<html><body style='background-color:#AED6F1;'><style>h1{color:#1B4F72}h2{color:#196F3D}</style><center><h1>Your Weather</h1><h2> brought to you by <strong>Mori and Zoe</strong></h2><p><img src='./clipsunshine.jpg' alt='sunshine' width='200'></p><h1>Your zipcode: %s </h1><h2>the temperature outside:</h2></center></body></html>", zipcode, zipcode);
//<img scr="https://goo.gl/images/pvkvYJ" alt="sunshine" width="100">
//<h1>You zipcode:</h1>
//<style>h1{color:#138D75}p{color:#2471A3}</style>
/* Generate the HTTP response */
printf("Connection: close\r\n");
printf("Content-length: %d\r\n", (int)strlen(content));
printf("Content-type: text/html\r\n\r\n");
printf("%s", content);
fflush(stdout);
exit(0);
}