00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <time.h>
00039 #ifndef _WIN32
00040 #include <unistd.h>
00041 #include "config.h"
00042 #else
00043 #include "winconfig.h"
00044 #endif
00045 #include "include/connection.h"
00046 #include "include/mtp_http.h"
00047 #include "include/macros.h"
00048 #include "include/mc_error.h"
00049 #include "include/message.h"
00050 #include "include/dynstring.h"
00051
00052 int
00053 mtp_http_Destroy(mtp_http_p http)
00054 {
00055 int i;
00056 #define SAFE_FREE(elem) \
00057 if(elem) \
00058 free(elem)
00059
00060 SAFE_FREE(http->http_version);
00061 SAFE_FREE(http->host);
00062 SAFE_FREE(http->return_code);
00063 SAFE_FREE(http->target);
00064 SAFE_FREE(http->date);
00065 SAFE_FREE(http->server);
00066 SAFE_FREE(http->accept_ranges);
00067 SAFE_FREE(http->content_length);
00068 SAFE_FREE(http->connection);
00069 SAFE_FREE(http->content_type);
00070 SAFE_FREE(http->user_agent);
00071 if(http->content != NULL) {
00072 for(i = 0; i < http->message_parts; i++) {
00073 SAFE_FREE(http->content[i].content_type);
00074 SAFE_FREE(http->content[i].data);
00075 }
00076 }
00077 SAFE_FREE(http->content);
00078 SAFE_FREE(http->boundary);
00079 SAFE_FREE(http->response_string);
00080 SAFE_FREE(http);
00081 #undef SAFE_FREE
00082 return 0;
00083 }
00084
00085 mtp_http_p
00086 mtp_http_New(void)
00087 {
00088 mtp_http_p http;
00089 http = (mtp_http_p)malloc(sizeof(mtp_http_t));
00090 CHECK_NULL(http, exit(0););
00091 memset(http, 0, sizeof(mtp_http_t));
00092 http->content = NULL;
00093 return http;
00094 }
00095
00096 int
00097 mtp_http_InitializeFromConnection
00098 (
00099 mtp_http_p http,
00100 connection_p connection
00101 )
00102 {
00103 int i=1;
00104 int n = 0;
00105 dynstring_t* message_string;
00106 char *buffer;
00107 int message_size = 0;
00108 int received_len = 0;
00109
00110 mtp_http_t* http_header;
00111
00112 buffer = (char*) malloc(sizeof(char) * (SOCKET_INPUT_SIZE + 1));
00113 CHECK_NULL(buffer, exit(0););
00114 message_string = dynstring_New();
00115 buffer[0] = '\0';
00116
00117 while(1){
00118 #ifndef _WIN32
00119 n = recvfrom(connection->clientfd,
00120 (void *) buffer,
00121 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00122 0,
00123 (struct sockaddr *) 0,
00124 (socklen_t *) 0);
00125 #else
00126 n = recvfrom(connection->clientfd,
00127 (void *) buffer,
00128 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00129 0,
00130 (struct sockaddr *) 0,
00131 0);
00132 #endif
00133 received_len += n;
00134 if (n < 0) {
00135 free(buffer);
00136 return MC_ERR_CONNECT;
00137 }
00138 else if (n == 0 || n < SOCKET_INPUT_SIZE) {
00139 if (n != 0) {
00140 buffer[n] = '\0';
00141 dynstring_Append(message_string, buffer);
00142 }
00143
00144
00145 http_header = mtp_http_New();
00146 if(mtp_http_ParseHeader(http_header, message_string->message) == NULL) {
00147 fprintf(stderr, "Error parsing HTTP Header. %s:%d\n",
00148 __FILE__, __LINE__);
00149 dynstring_Destroy(message_string);
00150 mtp_http_Destroy(http_header);
00151 free(buffer);
00152 return 2;
00153 }
00154 if ( received_len < (atoi(http_header->content_length) + http_header->header_length) ) {
00155 mtp_http_Destroy(http_header);
00156 continue;
00157 }
00158 mtp_http_Destroy(http_header);
00159
00160 free(buffer);
00161 if(mtp_http_Parse(http, message_string->message)) {
00162
00163 buffer = malloc
00164 (
00165 sizeof(char) *
00166 (
00167 strlen
00168 (
00169 "HTTP/1.0 503 Service Unavailable\r\nConnection: Close\r\n\r\nMobile C"
00170 )+1
00171 )
00172 );
00173 strcpy
00174 (
00175 buffer,
00176 "HTTP/1.0 503 Service Unavailable\r\nConnection: Close\r\n\r\nMobile C"
00177 );
00178 send
00179 (
00180 connection->clientfd,
00181 (void*)buffer,
00182 sizeof(char)*(strlen(buffer)),
00183 0
00184 );
00185 dynstring_Destroy(message_string);
00186 free(buffer);
00187 return ERR;
00188 } else {
00189 dynstring_Destroy(message_string);
00190 }
00191
00192 if (n < SOCKET_INPUT_SIZE) {
00193 buffer = strdup("HTTP/1.0 200 OK\r\nConnection: Close\r\n\r\nMobile C");
00194 send(
00195 connection->clientfd,
00196 (void*)buffer,
00197 sizeof(char)*strlen(buffer),
00198 0 );
00199 #ifdef _WIN32
00200 closesocket(connection->clientfd);
00201 #else
00202 close(connection->clientfd);
00203 #endif
00204 free(buffer);
00205 }
00206 break;
00207 } else {
00208 message_size += n;
00209 buffer[n] = '\0';
00210 i++;
00211 dynstring_Append(message_string, buffer);
00212 buffer[0] = '\0';
00213 if (!strcmp
00214 (
00215 message_string->message + message_size - 4,
00216 "\r\n\r\n"
00217 )
00218 )
00219 break;
00220 }
00221 }
00222 return 0;
00223 }
00224
00225 const char* http_GetExpression(const char* string, char** expr)
00226 {
00227 int i;
00228 int j;
00229 int success_flag = 0;
00230 const char* next_expr_ptr;
00231
00232 if(
00233 (string[0] == '\n') ||
00234 (string[0] == '\r' && string[1] == '\n')
00235 )
00236 {
00237 for(i = 0; string[i] == '\n' || string[i] == '\r'; i++);
00238 *expr = NULL;
00239 return string+i;
00240 }
00241
00242 for(i = 0;string[i] != '\0';i++) {
00243 if (
00244 (
00245 (string[i] == '\r') &&
00246 (string[i+1] == '\n') &&
00247 (string[i+2] != '\t') &&
00248 (string[i+2] != ' ')
00249 )
00250 ||
00251 (
00252 string[i] == '\n' &&
00253 string[i+1] != '\t' &&
00254 string[i+2] != ' '
00255 )
00256 )
00257 {
00258 success_flag = 1;
00259 break;
00260 }
00261 }
00262 if(success_flag)
00263 {
00264 *expr = (char*)malloc
00265 (
00266 sizeof(char) * (i+1)
00267 );
00268 for(j = 0; j < i; j++) {
00269 (*expr)[j] = string[j];
00270 }
00271 (*expr)[j] = '\0';
00272 next_expr_ptr = &(string[i]);
00273 if(next_expr_ptr[0] == '\r' && next_expr_ptr[1] == '\n') {
00274 next_expr_ptr += 2;
00275 } else if (next_expr_ptr[0] == '\n') {
00276 next_expr_ptr++;
00277 }
00278 return next_expr_ptr;
00279 } else {
00280 return NULL;
00281 }
00282 }
00283
00284 int http_ParseExpression(
00285 const char* expression_string,
00286 char** name,
00287 char** value
00288 )
00289 {
00290 int i=0;
00291 const char* tmp;
00292 const char* charptr;
00293 if(expression_string == NULL) {
00294 *name = NULL;
00295 *value = NULL;
00296 return MC_ERR_PARSE;
00297 }
00298 tmp = expression_string;
00299 if (tmp == NULL || (!strncmp(tmp, "\r\n", 2)) || (!strncmp(tmp, "\n", 1))) {
00300 *name = NULL;
00301 *value = NULL;
00302 return MC_ERR_PARSE;
00303 }
00304 for(; *tmp!=':' && *tmp!='\0'; tmp++)
00305 i++;
00306 if(*tmp == '\0') {
00307 *name = NULL;
00308 *value = NULL;
00309 return MC_ERR_PARSE;
00310 }
00311 *name = (char*)malloc
00312 (
00313 sizeof(char) * (i+1)
00314 );
00315 CHECK_NULL(*name, exit(0););
00316 charptr = expression_string;
00317 i=0;
00318 while(charptr != tmp) {
00319 (*name)[i] = *charptr;
00320 i++;
00321 charptr++;
00322 }
00323 (*name)[i] = '\0';
00324
00325 tmp++;
00326 while
00327 (
00328 (*tmp == ' ') ||
00329 (*tmp == '\t')
00330 )
00331 tmp++;
00332
00333 *value = (char*)malloc
00334 (
00335 sizeof(char) *
00336 (strlen(tmp) + 1)
00337 );
00338 CHECK_NULL(*value, exit(0););
00339 strcpy(*value, tmp);
00340 return MC_SUCCESS;
00341 }
00342
00343 const char* mtp_http_ParseHeader(struct mtp_http_s* http, const char* string)
00344 {
00345 const char* line = NULL;
00346 char* expr = NULL;
00347 char* name = NULL;
00348 char* value = NULL;
00349
00350 int err_code = 0;
00351
00352 line = string;
00353 line = http_ParseRequest
00354 (
00355 http,
00356 line
00357 );
00358 do
00359 {
00360 line = http_GetExpression
00361 (
00362 line,
00363 &expr
00364 );
00365
00366 err_code = http_ParseExpression
00367 (
00368 expr,
00369 &name,
00370 &value
00371 );
00372 if
00373 (
00374 (name == NULL) ||
00375 (value == NULL)
00376 )
00377 {
00378 if (expr != NULL) {
00379 free(expr);
00380 }
00381 break;
00382 }
00383 #define HTTP_PARSE_EXPR( parse_name, struct_name ) \
00384 if ( !strcmp(name, parse_name) ) { \
00385 http->struct_name = (char*)malloc \
00386 ( \
00387 sizeof(char) * \
00388 (strlen(value)+1) \
00389 ); \
00390 strcpy(http->struct_name, value); \
00391 } else
00392
00393 HTTP_PARSE_EXPR( "Host", host )
00394 HTTP_PARSE_EXPR( "Date", date )
00395 HTTP_PARSE_EXPR( "Server", server )
00396 HTTP_PARSE_EXPR( "Accept-Ranges", accept_ranges )
00397 HTTP_PARSE_EXPR( "Content-Length", content_length)
00398 HTTP_PARSE_EXPR( "Connection", connection )
00399 HTTP_PARSE_EXPR( "Content-Type", content_type)
00400 HTTP_PARSE_EXPR( "User-Agent", user_agent)
00401 HTTP_PARSE_EXPR( "Cache-Control", cache_control)
00402 HTTP_PARSE_EXPR( "MIME-Version", mime_version)
00403 HTTP_PARSE_EXPR( "Mime-Version", mime_version)
00404 {
00405 fprintf(stderr, "Warning: Unknown http name: %s. %s:%d\n",
00406 name, __FILE__, __LINE__);
00407 }
00408 #undef HTTP_PARSE_EXPR
00409
00410 #define SAFE_FREE( object ) \
00411 if(object) free(object); \
00412 object = NULL
00413
00414 SAFE_FREE(expr);
00415 SAFE_FREE(name);
00416 SAFE_FREE(value);
00417 #undef SAFE_FREE
00418
00419 } while(line != NULL && err_code == MC_SUCCESS);
00420
00421 http->header_length = line - string - 1;
00422 return line;
00423 }
00424
00425 int
00426 mtp_http_Parse(struct mtp_http_s* http, const char* string)
00427 {
00428 const char* line;
00429 char* expr = NULL;
00430 char* name = NULL;
00431 char* value = NULL;
00432 char* tmp;
00433 char* tmp2;
00434 int i;
00435
00436 int err_code = 0;
00437
00438 line = mtp_http_ParseHeader(http, string);
00439
00440
00441 if(
00442 http->content_type != NULL &&
00443 !strncmp(
00444 http->content_type,
00445 "multipart/mixed",
00446 strlen("multipart/mixed")
00447 )
00448 )
00449 {
00450 tmp = strstr(http->content_type, "boundary=");
00451 tmp += strlen("boundary=.");
00452 tmp2 = strchr(tmp, '\"');
00453 http->boundary = (char*)malloc(sizeof(char) * (tmp2 - tmp + 3));
00454
00455 http->boundary[0] = '-';
00456 http->boundary[1] = '-';
00457 for (i = 0; tmp != tmp2; i++, tmp++) {
00458 http->boundary[i+2] = *tmp;
00459 }
00460 http->boundary[i+2] = '\0';
00461
00462
00463 tmp = (char*)line;
00464 http->message_parts = 0;
00465 while((tmp = strstr(tmp, http->boundary))) {
00466 http->message_parts++;
00467 tmp++;
00468 }
00469 http->message_parts--;
00470 } else {
00471 http->boundary = NULL;
00472 http->message_parts = 1;
00473 }
00474
00475 if (http->message_parts == 1) {
00476 http->content = (struct mtp_http_content_s*)malloc(sizeof(struct mtp_http_content_s));
00477 memset(http->content, 0, sizeof(struct mtp_http_content_s));
00478
00479 if (line != NULL) {
00480 http->content->data = (void*)malloc
00481 (
00482 sizeof(char) *
00483 (strlen(line)+1)
00484 );
00485 strcpy((char*)http->content->data, line);
00486 if (http->content_type != NULL) {
00487 http->content->content_type = strdup(http->content_type);
00488 }
00489 }
00490 } else {
00491 http->content = (struct mtp_http_content_s*)malloc(
00492 sizeof(struct mtp_http_content_s) * http->message_parts );
00493 memset(http->content, 0, sizeof(struct mtp_http_content_s) * http->message_parts);
00494
00495 line = strstr(line, http->boundary);
00496 line += strlen(http->boundary);
00497 line = strchr(line, '\n');
00498 line++;
00499 for(i = 0; i < http->message_parts; i++) {
00500
00501
00502
00503
00504
00505
00506
00507 tmp = strstr(line + strlen(http->boundary), http->boundary);
00508
00509 do{
00510
00511
00512 line = http_GetExpression
00513 (
00514 line,
00515 &expr
00516 );
00517
00518 err_code = http_ParseExpression
00519 (
00520 expr,
00521 &name,
00522 &value
00523 );
00524 if
00525 (
00526 (name == NULL) ||
00527 (value == NULL)
00528 )
00529 {
00530 if (expr != NULL) {
00531 free(expr);
00532 }
00533 break;
00534 }
00535 if (!strcmp(name, "Content-Type")) {
00536 http->content[i].content_type = (char*)malloc(
00537 sizeof(char) * (strlen(value)+1));
00538 strcpy(http->content[i].content_type, value);
00539 }
00540
00541
00542 if (expr != NULL) {
00543 free(expr);
00544 expr = NULL;
00545 }
00546 if (name != NULL) {
00547 free(name);
00548 name = NULL;
00549 }
00550 if (value != NULL) {
00551 free(value);
00552 value = NULL;
00553 }
00554 } while(line != NULL && err_code == MC_SUCCESS);
00555
00556 http->content[i].data = (void*)malloc(tmp-line+sizeof(char));
00557 memcpy(http->content[i].data, line, tmp-line);
00558 ((char*)http->content[i].data)[tmp-line] = '\0';
00559
00560 line = tmp + strlen(http->boundary);
00561 line = strchr(line, '\n');
00562 line++;
00563 }
00564 }
00565 if (
00566 (http->http_performative == HTTP_POST) ||
00567 (http->http_performative == HTTP_PUT) ||
00568 (http->http_performative == HTTP_RESPONSE)
00569 )
00570 return 0;
00571 else
00572 return 1;
00573 }
00574
00575 const char* http_ParseRequest(
00576 mtp_http_p http,
00577 const char* string )
00578 {
00579 const char* cur;
00580 char* token;
00581 char* tmp = NULL;
00582 char* target;
00583 int len;
00584
00585 cur = string;
00586 cur = http_GetToken(cur, &token);
00587 if (token == NULL) {
00588 return NULL;
00589 }
00590 if (!strcmp(token, "GET")) {
00591 http->http_performative = HTTP_GET;
00592 cur = http_GetToken(cur, &tmp);
00593
00594 if(tmp) free(tmp);
00595 } else if(!strcmp(token, "HEAD")) {
00596
00597 http->http_performative = HTTP_HEAD;
00598 } else if(!strcmp(token, "POST")) {
00599 http->http_performative = HTTP_POST;
00600 cur = http_GetToken(cur, &tmp);
00601 if(tmp != NULL) {
00602 if(!strncmp(tmp, "http://",7)) {
00603 target = strchr(tmp+7, (int)'/');
00604 } else {
00605 target = strchr(tmp, (int)'/');
00606 }
00607 if (target == NULL)
00608 target = tmp;
00609 http->target = (char*) malloc(sizeof(char) * (strlen(target)+1));
00610 strcpy(http->target, target);
00611 free(tmp);
00612 }
00613 } else if(!strcmp(token, "PUT")) {
00614 http->http_performative = HTTP_PUT;
00615 cur = http_GetToken(cur, &tmp);
00616 if (tmp != NULL) {
00617 http->target = (char*)malloc(sizeof(char)*(strlen(tmp)+1));
00618 strcpy(http->target, tmp);
00619 free(tmp);
00620 }
00621 } else if(!strcmp(token, "DELETE")) {
00622 http->http_performative = HTTP_DELETE;
00623 } else if(!strcmp(token, "TRACE")) {
00624 http->http_performative = HTTP_TRACE;
00625 } else if(!strcmp(token, "OPTIONS")) {
00626 http->http_performative = HTTP_OPTIONS;
00627 } else if(!strcmp(token, "CONNECT")) {
00628 http->http_performative = HTTP_CONNECT;
00629 } else if(!strncmp(token, "HTTP/", 5)) {
00630
00631 http->http_performative = HTTP_RESPONSE;
00632
00633 free(token);
00634 cur = http_GetToken(cur, &token);
00635 sscanf(token, "%d", &http->response_code);
00636
00637 len = strstr(cur, "\r\n") - cur + 1;
00638 http->response_string = malloc(
00639 sizeof(char) * (len +1) );
00640 strncpy(
00641 http->response_string,
00642 cur,
00643 len );
00644 http->response_string[len] = '\0';
00645 } else {
00646
00647
00648 http->http_performative = HTTP_PERFORMATIVE_UNDEF;
00649 }
00650 free(token);
00651 cur = string;
00652 while(*cur != '\0') {
00653 if(*cur == '\n') {
00654 while (*cur == '\n' || *cur == '\r' || *cur == ' ')
00655 cur++;
00656 break;
00657 }
00658 cur++;
00659 }
00660 return cur;
00661 }
00662
00663 const char*
00664 http_GetToken(const char* string, char** token)
00665 {
00666 const char* cur;
00667 const char* begin;
00668 char* itr;
00669
00670 cur = string;
00671
00672 if (string[0] == '\r' && string[1] == '\n') {
00673 *token = NULL;
00674 return NULL;
00675 }
00676
00677 while(*cur == ' ' || *cur == '\t' || *cur == '\r' || *cur == '\n') cur++;
00678
00679 begin = cur;
00680 while(*cur != '\0') {
00681 cur++;
00682 if (*cur == ' ' || *cur == '\t' || *cur == '\r' || *cur == '\n')
00683 break;
00684 }
00685 cur--;
00686 *token = (char*)malloc(cur - begin + 4*sizeof(char));
00687 itr = *token;
00688 while (begin <= cur) {
00689 *itr = *begin;
00690 itr++;
00691 begin++;
00692 }
00693 *itr='\0';
00694 return cur+1;
00695 }
00696
00697 int mtp_http_ParseResponse(struct mtp_http_s* http, const char* string)
00698 {
00699
00700 }
00701
00702 int
00703 mtp_http_ComposeMessage(message_p message)
00704 {
00705 char* http_header;
00706 char* tmp;
00707 char buf[20];
00708 if (message->isHTTP) {
00709
00710 return 0;
00711 }
00712
00713 http_header = (char*)malloc
00714 (
00715 sizeof(char) * (1400 + strlen(message->to_address))
00716 );
00717 http_header[0] = '\0';
00718 strcat(http_header, "POST /");
00719 strcat(http_header, message->target);
00720 strcat(http_header, " HTTP/1.0\r\n");
00721 strcat(http_header, "User-Agent: MobileC/");
00722 strcat(http_header, PACKAGE_VERSION);
00723 strcat(http_header, "\r\n");
00724 strcat(http_header, "Host: ");
00725 strcat(http_header, message->to_address);
00726 strcat(http_header, "\r\n");
00727 strcat(http_header, "Content-Type: text/plain\r\n");
00728 strcat(http_header, "Connection: Close\r\n");
00729 strcat(http_header, "Content-Length: ");
00730 sprintf(buf, "%d", strlen(message->message_body) + 1);
00731 strcat(http_header, buf);
00732 strcat(http_header, "\r\n\r\n");
00733
00734 tmp = (char*)malloc
00735 (
00736 sizeof(char) *
00737 (strlen(http_header) + strlen(message->message_body) + 1)
00738 );
00739 tmp[0] = '\0';
00740 strcpy(tmp, http_header);
00741 strcat(tmp, message->message_body);
00742 free(message->message_body);
00743 message->message_body = tmp;
00744 free(http_header);
00745 return MC_SUCCESS;
00746 }
00747
00748 struct message_s*
00749 mtp_http_CreateMessage(
00750 mtp_http_t* mtp_http,
00751 char* hostname,
00752 int port)
00753 {
00754 int i;
00755 int num;
00756 char buf[30];
00757 char boundary[30];
00758 message_t* message;
00759 dynstring_t* http_header;
00760 dynstring_t* http_body;
00761 http_header = dynstring_New();
00762 http_body = dynstring_New();
00763 dynstring_Append(http_header, "POST /");
00764 dynstring_Append(http_header, mtp_http->target);
00765 dynstring_Append(http_header, " HTTP/1.1\r\n");
00766 dynstring_Append(http_header, "User-Agent: MobileC/");
00767 dynstring_Append(http_header, PACKAGE_VERSION);
00768 dynstring_Append(http_header, "\r\n");
00769 dynstring_Append(http_header, "Host: ");
00770 dynstring_Append(http_header, mtp_http->host);
00771 dynstring_Append(http_header, ":");
00772 sprintf(buf, "%d", port);
00773 dynstring_Append(http_header, buf);
00774 dynstring_Append(http_header, "\r\n");
00775 dynstring_Append(http_header, "Cache-Control: no-cache\r\n");
00776 dynstring_Append(http_header, "Mime-Version: 1.0\r\n");
00777
00778
00779
00780 if(mtp_http->message_parts == 1) {
00781 dynstring_Append(http_header, "Content-Type: text/plain\r\n");
00782 dynstring_Append(http_header, "Content-Length: ");
00783 sprintf(buf, "%d", strlen((char*)mtp_http->content[0].data));
00784 dynstring_Append(http_header, buf);
00785 dynstring_Append(http_header, "\r\n\r\n");
00786 dynstring_Append(http_header, (char*)mtp_http->content[0].data);
00787 } else {
00788
00789 srand(time(NULL));
00790 strcpy(boundary, "--");
00791 for(i = 2; i < 26; i++) {
00792 num = rand() % 36;
00793 if(num < 10) {
00794 boundary[i] = (char)(48 + num);
00795 } else {
00796 boundary[i] = (char)( (num-10)+65);
00797 }
00798 }
00799 boundary[i] = '\0';
00800
00801
00802
00803 dynstring_Append(http_body, "This is not part of the MIME multipart encoded message.\r\n");
00804 for(i = 0; i<mtp_http->message_parts; i++) {
00805 dynstring_Append(http_body, boundary);
00806 dynstring_Append(http_body, "\r\nContent-Type: ");
00807 dynstring_Append(http_body, mtp_http->content[i].content_type);
00808 dynstring_Append(http_body, "\r\n\r\n");
00809 dynstring_Append(http_body, (char*)mtp_http->content[i].data);
00810 dynstring_Append(http_body, "\r\n");
00811 }
00812
00813 dynstring_Append(http_body, boundary);
00814 dynstring_Append(http_body, "--");
00815 dynstring_Append(http_body, "\r\n\r\n");
00816
00817
00818 dynstring_Append(http_header, "Content-Length: ");
00819 sprintf(buf, "%d", http_body->len-2 );
00820 dynstring_Append(http_header, buf);
00821 dynstring_Append(http_header, "\r\n");
00822 dynstring_Append(http_header, "Content-Type: multipart/mixed ; boundary=\"");
00823 dynstring_Append(http_header, boundary+2);
00824 dynstring_Append(http_header, "\"\r\n");
00825
00826 }
00827 dynstring_Append(http_header, "Connection: Close\r\n\r\n");
00828 message = message_New();
00829 message->to_address = (char*)malloc(
00830 sizeof(char) * (strlen(hostname)+15) );
00831 sprintf(message->to_address, "%s:%d", hostname, port);
00832 message->message_body = (char*) malloc(
00833 sizeof(char) * (http_header->len + http_body->len + 1));
00834 strcpy(message->message_body, http_header->message);
00835 strcat(message->message_body, http_body->message);
00836 dynstring_Destroy(http_header);
00837 dynstring_Destroy(http_body);
00838 message->isHTTP = 1;
00839 return message;
00840 }
00841