-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweatherAPI.c
More file actions
184 lines (143 loc) · 5.45 KB
/
weatherAPI.c
File metadata and controls
184 lines (143 loc) · 5.45 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
/*
* 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
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);
// THIS IS WHERE ECHOclient COMES IN
int clientfd;
char *host, *port, buf[MAXLINE];
rio_t rio;
host = "http://www.openweathermap.org";
//data/2.5/weather?zip=19004,us&APPID=364ebbea13d49a5e8e505870368c78be";
port = "80";
clientfd = Open_clientfd(host, port);
Rio_readinitb(&rio,clientfd);
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);
}
#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
//const char* command = "wget -O results.txt urlBegin";
//int result = 0;
//result = system(command);
//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, "%sThanks for visiting!\r\n", content);
/* 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);
}