| 78 jabber_bosh_connection_send(conn, init); |
78 jabber_bosh_connection_send(conn, init); |
| 79 } |
79 } |
| 80 |
80 |
| 81 void jabber_bosh_connection_login_cb(PurpleHTTPRequest *req, PurpleHTTPResponse *res, void *userdata) { |
81 void jabber_bosh_connection_login_cb(PurpleHTTPRequest *req, PurpleHTTPResponse *res, void *userdata) { |
| 82 purple_debug_info("jabber", "RECEIVED FIRST HTTP RESPONSE\n"); |
82 purple_debug_info("jabber", "RECEIVED FIRST HTTP RESPONSE\n"); |
| |
83 printf("\nDATA\n\n%s\n", res->data); |
| 83 } |
84 } |
| 84 |
85 |
| 85 void jabber_bosh_connection_send(PurpleBOSHConnection *conn, xmlnode *node) { |
86 void jabber_bosh_connection_send(PurpleBOSHConnection *conn, xmlnode *node) { |
| 86 PurpleHTTPRequest *request = g_new0(PurpleHTTPRequest, 1); |
87 PurpleHTTPRequest *request = g_new0(PurpleHTTPRequest, 1); |
| 87 jabber_bosh_http_request_init(request, "POST", g_strdup_printf("/%s", conn->path), jabber_bosh_connection_login_cb, conn); |
88 jabber_bosh_http_request_init(request, "POST", g_strdup_printf("/%s", conn->path), jabber_bosh_connection_login_cb, conn); |
| 102 conn->conn_a->connect_cb = jabber_bosh_connection_connected; |
103 conn->conn_a->connect_cb = jabber_bosh_connection_connected; |
| 103 jabber_bosh_http_connection_connect(conn->conn_a); |
104 jabber_bosh_http_connection_connect(conn->conn_a); |
| 104 } |
105 } |
| 105 |
106 |
| 106 static void jabber_bosh_http_connection_receive(gpointer data, gint source, PurpleInputCondition condition) { |
107 static void jabber_bosh_http_connection_receive(gpointer data, gint source, PurpleInputCondition condition) { |
| |
108 char buffer[1024]; |
| |
109 int len; |
| 107 PurpleHTTPConnection *conn = data; |
110 PurpleHTTPConnection *conn = data; |
| 108 PurpleHTTPResponse *response = conn->current_response; |
111 PurpleHTTPResponse *response = conn->current_response; |
| 109 |
112 |
| 110 purple_debug_info("jabber", "jabber_bosh_http_connection_receive\n"); |
113 purple_debug_info("jabber", "jabber_bosh_http_connection_receive\n"); |
| 111 if (response) { |
114 if (!response) { |
| 112 // data for current response |
|
| 113 |
|
| 114 } else { |
|
| 115 // new response |
115 // new response |
| 116 response = conn->current_response = g_new0(PurpleHTTPResponse, 1); |
116 response = conn->current_response = g_new0(PurpleHTTPResponse, 1); |
| |
117 jabber_bosh_http_response_init(response); |
| |
118 } |
| |
119 |
| |
120 len = read(source, buffer, 1024); |
| |
121 if (len > 0) { |
| |
122 char *found = NULL; |
| |
123 if (found = g_strstr_len(buffer, len, "\r\n\r\n")) { |
| |
124 char *beginning = buffer; |
| |
125 char *field = NULL; |
| |
126 char *value = NULL; |
| |
127 char *cl = NULL; |
| |
128 |
| |
129 while (*beginning != 'H') ++beginning; |
| |
130 beginning[12] = 0; |
| |
131 response->status = atoi(&beginning[9]); |
| |
132 beginning = &beginning[13]; |
| |
133 do { |
| |
134 ++beginning; |
| |
135 } while (*beginning != '\n'); |
| |
136 ++beginning; |
| |
137 /* parse HTTP response header */ |
| |
138 for (;beginning != found; ++beginning) { |
| |
139 if (!field) field = beginning; |
| |
140 if (*beginning == ':') { |
| |
141 *beginning = 0; |
| |
142 value = beginning + 1; |
| |
143 } else if (*beginning == '\r') { |
| |
144 *beginning = 0; |
| |
145 g_hash_table_replace(response->header, g_strdup(field), g_strdup(value)); |
| |
146 value = field = 0; |
| |
147 ++beginning; |
| |
148 } |
| |
149 } |
| |
150 ++found; ++found; ++found; ++found; |
| |
151 |
| |
152 cl = g_hash_table_lookup(response->header, "Content-Length"); |
| |
153 if (cl) { |
| |
154 PurpleHTTPRequest *request = NULL; |
| |
155 response->data_len = atoi(cl); |
| |
156 if (response->data <= len - (found - buffer)) response->data = g_memdup(found, response->data_len); |
| |
157 else printf("\nDidn't receive complete content"); |
| |
158 |
| |
159 request = g_queue_pop_head(conn->requests); |
| |
160 if (request) { |
| |
161 request->cb(request, response, conn->userdata); |
| |
162 jabber_bosh_http_request_clean(request); |
| |
163 jabber_bosh_http_response_clean(response); |
| |
164 } else { |
| |
165 purple_debug_info("jabber", "received HTTP response but haven't requested anything yet.\n"); |
| |
166 } |
| |
167 } else { |
| |
168 printf("\ndidn't receive length.\n"); |
| |
169 } |
| |
170 } else { |
| |
171 printf("\nDid not receive complete HTTP header!\n"); |
| |
172 } |
| |
173 } else { |
| |
174 purple_debug_info("jabber", "jabber_bosh_http_connection_receive: problem receiving data\n"); |
| 117 } |
175 } |
| 118 } |
176 } |
| 119 |
177 |
| 120 void jabber_bosh_http_connection_init(PurpleHTTPConnection *conn, PurpleAccount *account, char *host, int port) { |
178 void jabber_bosh_http_connection_init(PurpleHTTPConnection *conn, PurpleAccount *account, char *host, int port) { |
| 121 conn->account = account; |
179 conn->account = account; |
| 122 conn->host = host; |
180 conn->host = host; |
| 123 conn->port = port; |
181 conn->port = port; |
| 124 conn->connect_cb = NULL; |
182 conn->connect_cb = NULL; |
| 125 conn->current_response = NULL; |
183 conn->current_response = NULL; |
| |
184 conn->current_data = NULL; |
| 126 conn->requests = g_queue_new(); |
185 conn->requests = g_queue_new(); |
| 127 } |
186 } |
| 128 |
187 |
| 129 void jabber_bosh_http_connection_clean(PurpleHTTPConnection *conn) { |
188 void jabber_bosh_http_connection_clean(PurpleHTTPConnection *conn) { |
| 130 g_queue_free(conn->requests); |
189 g_queue_free(conn->requests); |