1 /*
2  * copied and amended from a simple http socket program:
3  * "Simple Internet client program - by Dan Drown <abob@linux.com>"
4  * available (last I looked) at:
5  *      http://linux.omnipotent.net/article.php?article_id=5424
6  */
7 
8 #include <sys/types.h>  /* for socket,connect */
9 #include <sys/socket.h> /* for socket,connect */
10 #include <netinet/in.h> /* for htons */
11 #include <netdb.h>      /* for gethostbyname */
12 #include <string.h>     /* for memcpy */
13 #include <stdio.h>      /* for perror */
14 #include <stdlib.h>     /* for exit */
15 #include <unistd.h>     /* for read,write */
16 #include <stdarg.h>     /* needed for argument processing in debug */
17 #include <time.h>       /* needed for getting timestamp in debug */
18 
19 #ifdef GOTSSL
20 #include <openssl/crypto.h>
21 #include <openssl/x509.h>
22 #include <openssl/pem.h>
23 #include <openssl/ssl.h>
24 #include <openssl/err.h>
25 #endif
26 
27 
28 // how big is everything
29 #define MAX_STR 1025
30 #define MAX_HDR_STR 2048
31 #define MAX_BUFFERS 1025
32 #define MAX_HEADERS 257
33 #define READ_BUF 80
34 
35 // fake up a definition of bool if it doesnt exist
36 #ifndef bool
37 typedef unsigned char    bool;
38 #endif
39 
40 // create my true and false
41 #ifndef false
42 typedef enum { false, true } mybool;
43 #endif
44 
45 
46 #ifdef DOHERROR
47 void herror(char * str);
48 #endif
49 
50 
51 struct mhttp_conn_st
52 {
53     char       *host;
54     int        port;
55     bool       is_ssl;
56     bool       is_chunked;
57     int        fd;
58 #ifdef GOTSSL
59     SSL_CTX*   ctx;
60     SSL*       ssl;
61     SSL_METHOD *meth;
62     X509*      server_cert;
63 #endif
64 };
65 
66 typedef struct mhttp_conn_st *mhttp_conn_t;
67 
68 
69 
70 char *construct_request(char *action, char *url);
71 
72 mhttp_conn_t mhttp_new_conn(void);
73 
74 void mhttp_end_conn(mhttp_conn_t conn);
75 
76 #ifdef GOTSSL
77 static int mhttp_verify_callback(int ok, X509_STORE_CTX* ctx);
78 #endif
79 
80 int write_socket(mhttp_conn_t conn, const void *buf, size_t count);
81 
82 int read_socket(mhttp_conn_t conn, void *buf);
83 
84 int read_headers(mhttp_conn_t conn, char *str);
85 
86 int find_content_length(void);
87 
88 bool find_transfer_encoding(void);
89 
90 int find_chunk(mhttp_conn_t conn, char **ptr, int *rem);
91 
92 int check_url(char *purl, char **url, char **host);
93 
94 int check_action(char *paction, char **action);
95 
96 int get_port_and_uri(char *url, char *host, char **surl);
97 
98 int mhttp_build_inet_addr(struct sockaddr_in *addr, const char *hostname, unsigned short int port);
99 
100 int mhttp_connect_inet_addr(const char *hostname, unsigned short int port);
101 
102 void mhttp_switch_debug(int set);
103 
104 void mhttp_reset(void);
105 
106 void mhttp_init(void);
107 
108 void mhttp_add_header(char *hdr);
109 
110 void mhttp_set_protocol(int proto);
111 
112 void mhttp_set_body(char *bdy);
113 
114 char *mhttp_get_response_headers(void);
115 
116 char *mhttp_get_reason(void);
117 
118 char *mhttp_get_response(void);
119 
120 int mhttp_call(char *paction, char *purl);
121 
122 int mhttp_get_status_code(void);
123 
124 int mhttp_get_response_length(void);
125 
126 void mhttp_debug(const char *msgfmt, ...);
127 
128