-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanWeatherAPI
More file actions
87 lines (65 loc) · 2.81 KB
/
CleanWeatherAPI
File metadata and controls
87 lines (65 loc) · 2.81 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
/*
* 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 main(int argc, char **argv){
char *urlBegin, *urlEnd, *s, *input;
char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE], zipcode[MAXLINE];
int n1=0, n2=0;
int zip;
/* Extract the zipCode */
input = strncat(getenv("QUERY_STRING"), "\0", 5);
fprintf(stderr, "this is input: %s\n", input);
sscanf(input, "zipcode=%d", &zip);
fprintf(stderr, "\nthe zipcode you entered is: %d\n", zip);
sprintf(zipcode, "%d", zip);
fprintf(stderr, "this is zipcode: %s \n", zipcode);
//fprintf(stderr, "\nthis is null %s\n", zipcode[0]);
fprintf(stderr, "strlen is %d", strlen(zipcode));
// only ask server for weather is user entered a valid zipcode
if (strlen(zipcode) ==5){
fprintf(stderr, "\nzipcode has something in it!\n");
char *host, *port;
rio_t rio;
char buf[MAXLINE];
char buf2[MAXLINE];
int clientfd, n;
// THIS IS WHERE ECHOclient COMES IN
sprintf(buf, "GET /data/2.5/weather?zip=%s,us&units=imperial&APPID=364ebbea13d49a5e8e505870368c78be\n", zipcode);
host = "162.243.53.59";
//"192.241.169.168"
//http://www.openweathermap.org";
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, buf, MAXLINE);
Rio_readlineb(&rio, buf2, MAXLINE); /// buf2 has our output
Close(clientfd);
fprintf(stderr, "%s", buf2);
fprintf(stderr, "made it to the JSON output\n ");
char *response_split; // split response on 1st occurence
response_split = strchr(buf2, '[');
*response_split = '\0';
//strcpy(response, response_split +1);
}
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='sunshine.jpeg' alt='sunshine' width='200'></p><h1>Enter Your zipcode:</h1><form action='/cgi-bin/htmlVersion?'><input type='text' name='zipcode'><br><input type='submit' value='Submit'></form><h2>the temperature outside:</h2></center></body></html>", zipcode, zipcode);
/* 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);
}