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 rece_de_msg(char *buffer, connection_p con, char *privatekey){
00098
00099 FILE *fd, *tempfptr;
00100 struct stat stbuf;
00101 int mode,i,j;
00102
00103 char *infile;
00104 char *outfile;
00105 char ch;
00106
00107 #ifndef _WIN32
00108 int fd1;
00109 infile = (char *)malloc(sizeof(char)*20);
00110 strcpy(infile, "en-msgXXXXXX");
00111 fd1 = mkstemp(infile);
00112 if (fd1 == -1) {
00113 fprintf(stderr, "Could not create temporary file:%s. %s:%d\n",
00114 infile, __FILE__, __LINE__ );
00115 exit(EXIT_FAILURE);
00116 }
00117 close(fd1);
00118
00119 outfile = (char *)malloc(sizeof(char)*20);
00120 strcpy(outfile, "enXXXXXX");
00121 fd1 = mkstemp(outfile);
00122 if (fd1 == -1) {
00123 fprintf(stderr, "Could not create temporary file:%s. %s:%d\n",
00124 outfile, __FILE__, __LINE__ );
00125 exit(EXIT_FAILURE);
00126 }
00127 close(fd1);
00128 #else
00129 infile = _tempnam(".", "msg");
00130 outfile = _tempnam(".", "en-msg");
00131 #endif
00132
00133
00134 if (receive_AES_en_MA(con->clientfd, &(con->nonce), infile, privatekey ) != 1){
00135 printf("Server: Fail to successfully encrypted msg \n");
00136 if( remove(infile) ) printf("mtp_http.c : remove error 1");
00137 return -1;
00138 }
00139
00140
00141 mode = 1;
00142
00143 if (aes_en_de(mode, infile, outfile, con->AES_key, &(con->nonce), con->clientfd) != 1){
00144 printf("Server: AES Decryption failed \n");
00145 if( remove(infile) ) printf("mtp_http.c : remove error 2");
00146 if( remove(outfile) ) printf("mtp_http.c : remove error 3");
00147 return -1;
00148 }
00149
00150 if( (fd = fopen(outfile,"r")) == NULL ){
00151 printf("Cannot find %s file to read msg content", outfile);
00152 if( remove(infile) ) printf("mtp_http.c : remove error 4");
00153 if( remove(outfile) ) printf("mtp_http.c : remove error 5");
00154 return -1;
00155 }
00156 stat(outfile, &stbuf);
00157
00158
00159 j=0;
00160 for(i=0; i<(int)stbuf.st_size; i++ ){
00161
00162 ch = fgetc(fd);
00163 if(ch != '\r')
00164 buffer[j++]=ch;
00165 else
00166 buffer[j++]=' ';
00167 }
00168
00169 fclose(fd);
00170
00171 if( remove(infile) ) printf("mtp_http.c : remove error 6");
00172 if( remove(outfile) ) printf("mtp_http.c : remove error 7");
00173 return strlen(buffer);
00174 }
00175 int
00176 mtp_http_InitializeFromConnection
00177 (
00178 mtp_http_p http,
00179 connection_p connection,
00180 char *privatekey
00181 )
00182 {
00183 int i=1;
00184 int n = 0;
00185 dynstring_t* message_string;
00186 char *buffer;
00187 int message_size = 0;
00188 int received_len = 0;
00189
00190 mtp_http_t* http_header;
00191
00192 buffer = (char*) malloc(sizeof(char) * (SOCKET_INPUT_SIZE + 1));
00193 CHECK_NULL(buffer, exit(0););
00194 message_string = dynstring_New();
00195 memset(buffer, '\0', (sizeof(char) * (SOCKET_INPUT_SIZE + 1)) );
00196
00197 while(1){
00198 #ifdef NEW_SECURITY
00199 if ( (n = rece_de_msg(buffer, connection, privatekey)) == -1 ){
00200 printf("Error on receving en msg \n");
00201 free(buffer);
00202 return 2;
00203 }
00204 #else
00205 #ifndef _WIN32
00206 n = recvfrom(connection->clientfd,
00207 (void *) buffer,
00208 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00209 0,
00210 (struct sockaddr *) 0,
00211 (socklen_t *) 0);
00212
00213 #else
00214 n = recvfrom(connection->clientfd,
00215 (void *) buffer,
00216 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00217 0,
00218 (struct sockaddr *) 0,
00219 0);
00220 #endif
00221 #endif
00222 received_len += n;
00223 if (n < 0) {
00224 free(buffer);
00225 return MC_ERR_CONNECT;
00226 }
00227 else if (n == 0 || n < SOCKET_INPUT_SIZE) {
00228 if (n != 0) {
00229 buffer[n] = '\0';
00230 dynstring_Append(message_string, buffer);
00231 }
00232
00233
00234 http_header = mtp_http_New();
00235 if(mtp_http_ParseHeader(http_header, message_string->message) == NULL) {
00236 fprintf(stderr, "Error parsing HTTP Header. %s:%d\n",
00237 __FILE__, __LINE__);
00238 dynstring_Destroy(message_string);
00239 mtp_http_Destroy(http_header);
00240 free(buffer);
00241 return 2;
00242 }
00243 if ( received_len < (atoi(http_header->content_length) + http_header->header_length) ) {
00244 mtp_http_Destroy(http_header);
00245 continue;
00246 }
00247 mtp_http_Destroy(http_header);
00248
00249 free(buffer);
00250 if(mtp_http_Parse(http, message_string->message)) {
00251
00252 buffer = malloc
00253 (
00254 sizeof(char) *
00255 (
00256 strlen
00257 (
00258 "HTTP/1.0 503 Service Unavailable\r\nConnection: Close\r\n\r\nMobile C"
00259 )+1
00260 )
00261 );
00262 strcpy
00263 (
00264 buffer,
00265 "HTTP/1.0 503 Service Unavailable\r\nConnection: Close\r\n\r\nMobile C"
00266 );
00267 send
00268 (
00269 connection->clientfd,
00270 (void*)buffer,
00271 sizeof(char)*(strlen(buffer)),
00272 0
00273 );
00274 dynstring_Destroy(message_string);
00275 free(buffer);
00276 return ERR;
00277 } else {
00278 dynstring_Destroy(message_string);
00279 }
00280
00281 if (n < SOCKET_INPUT_SIZE) {
00282 buffer = strdup("HTTP/1.0 200 OK\r\nConnection: Close\r\n\r\nMobile C");
00283 send(
00284 connection->clientfd,
00285 (void*)buffer,
00286 sizeof(char)*strlen(buffer),
00287 0 );
00288 #ifdef _WIN32
00289 closesocket(connection->clientfd);
00290 #else
00291 close(connection->clientfd);
00292 #endif
00293 free(buffer);
00294 }
00295 break;
00296 } else {
00297 message_size += n;
00298 buffer[n] = '\0';
00299 i++;
00300 dynstring_Append(message_string, buffer);
00301 buffer[0] = '\0';
00302 if (!strcmp
00303 (
00304 message_string->message + message_size - 4,
00305 "\r\n\r\n"
00306 )
00307 )
00308 break;
00309 }
00310 }
00311 return 0;
00312 }
00313
00314 const char* http_GetExpression(const char* string, char** expr)
00315 {
00316 int i;
00317 int j;
00318 int success_flag = 0;
00319 const char* next_expr_ptr;
00320
00321 if(
00322 (string[0] == '\n') ||
00323 (string[0] == '\r' && string[1] == '\n')
00324 )
00325 {
00326 for(i = 0; string[i] == '\n' || string[i] == '\r'; i++);
00327 *expr = NULL;
00328 return string+i;
00329 }
00330
00331 for(i = 0;string[i] != '\0';i++) {
00332 if (
00333 (
00334 (string[i] == '\r') &&
00335 (string[i+1] == '\n') &&
00336 (string[i+2] != '\t') &&
00337 (string[i+2] != ' ')
00338 )
00339 ||
00340 (
00341 string[i] == '\n' &&
00342 string[i+1] != '\t' &&
00343 string[i+2] != ' '
00344 )
00345 )
00346 {
00347 success_flag = 1;
00348 break;
00349 }
00350 }
00351 if(success_flag)
00352 {
00353 *expr = (char*)malloc
00354 (
00355 sizeof(char) * (i+1)
00356 );
00357 for(j = 0; j < i; j++) {
00358 (*expr)[j] = string[j];
00359 }
00360 (*expr)[j] = '\0';
00361 next_expr_ptr = &(string[i]);
00362 if(next_expr_ptr[0] == '\r' && next_expr_ptr[1] == '\n') {
00363 next_expr_ptr += 2;
00364 } else if (next_expr_ptr[0] == '\n') {
00365 next_expr_ptr++;
00366 }
00367 return next_expr_ptr;
00368 } else {
00369 return NULL;
00370 }
00371 }
00372
00373 int http_ParseExpression(
00374 const char* expression_string,
00375 char** name,
00376 char** value
00377 )
00378 {
00379 int i=0;
00380 const char* tmp;
00381 const char* charptr;
00382 if(expression_string == NULL) {
00383 *name = NULL;
00384 *value = NULL;
00385 return MC_ERR_PARSE;
00386 }
00387 tmp = expression_string;
00388 if (tmp == NULL || (!strncmp(tmp, "\r\n", 2)) || (!strncmp(tmp, "\n", 1))) {
00389 *name = NULL;
00390 *value = NULL;
00391 return MC_ERR_PARSE;
00392 }
00393 for(; *tmp!=':' && *tmp!='\0'; tmp++)
00394 i++;
00395 if(*tmp == '\0') {
00396 *name = NULL;
00397 *value = NULL;
00398 return MC_ERR_PARSE;
00399 }
00400 *name = (char*)malloc
00401 (
00402 sizeof(char) * (i+1)
00403 );
00404 CHECK_NULL(*name, exit(0););
00405 charptr = expression_string;
00406 i=0;
00407 while(charptr != tmp) {
00408 (*name)[i] = *charptr;
00409 i++;
00410 charptr++;
00411 }
00412 (*name)[i] = '\0';
00413
00414 tmp++;
00415 while
00416 (
00417 (*tmp == ' ') ||
00418 (*tmp == '\t')
00419 )
00420 tmp++;
00421
00422 *value = (char*)malloc
00423 (
00424 sizeof(char) *
00425 (strlen(tmp) + 1)
00426 );
00427 CHECK_NULL(*value, exit(0););
00428 strcpy(*value, tmp);
00429 return MC_SUCCESS;
00430 }
00431
00432 const char* mtp_http_ParseHeader(struct mtp_http_s* http, const char* string)
00433 {
00434 const char* line = NULL;
00435 char* expr = NULL;
00436 char* name = NULL;
00437 char* value = NULL;
00438
00439 int err_code = 0;
00440
00441 line = string;
00442 line = http_ParseRequest
00443 (
00444 http,
00445 line
00446 );
00447 do
00448 {
00449 line = http_GetExpression
00450 (
00451 line,
00452 &expr
00453 );
00454
00455 err_code = http_ParseExpression
00456 (
00457 expr,
00458 &name,
00459 &value
00460 );
00461 if
00462 (
00463 (name == NULL) ||
00464 (value == NULL)
00465 )
00466 {
00467 if (expr != NULL) {
00468 free(expr);
00469 }
00470 break;
00471 }
00472 #define HTTP_PARSE_EXPR( parse_name, struct_name ) \
00473 if ( !strcmp(name, parse_name) ) { \
00474 http->struct_name = (char*)malloc \
00475 ( \
00476 sizeof(char) * \
00477 (strlen(value)+1) \
00478 ); \
00479 strcpy(http->struct_name, value); \
00480 } else
00481
00482 HTTP_PARSE_EXPR( "Host", host )
00483 HTTP_PARSE_EXPR( "Date", date )
00484 HTTP_PARSE_EXPR( "Server", server )
00485 HTTP_PARSE_EXPR( "Accept-Ranges", accept_ranges )
00486 HTTP_PARSE_EXPR( "Content-Length", content_length)
00487 HTTP_PARSE_EXPR( "Connection", connection )
00488 HTTP_PARSE_EXPR( "Content-Type", content_type)
00489 HTTP_PARSE_EXPR( "User-Agent", user_agent)
00490 HTTP_PARSE_EXPR( "Cache-Control", cache_control)
00491 HTTP_PARSE_EXPR( "MIME-Version", mime_version)
00492 HTTP_PARSE_EXPR( "Mime-Version", mime_version)
00493 {
00494 fprintf(stderr, "Warning: Unknown http name: %s. %s:%d\n",
00495 name, __FILE__, __LINE__);
00496 }
00497 #undef HTTP_PARSE_EXPR
00498
00499 #define SAFE_FREE( object ) \
00500 if(object) free(object); \
00501 object = NULL
00502
00503 SAFE_FREE(expr);
00504 SAFE_FREE(name);
00505 SAFE_FREE(value);
00506 #undef SAFE_FREE
00507
00508 } while(line != NULL && err_code == MC_SUCCESS);
00509
00510 http->header_length = line - string - 1;
00511 return line;
00512 }
00513
00514 int
00515 mtp_http_Parse(struct mtp_http_s* http, const char* string)
00516 {
00517 const char* line;
00518 char* expr = NULL;
00519 char* name = NULL;
00520 char* value = NULL;
00521 char* tmp;
00522 char* tmp2;
00523 int i;
00524
00525 int err_code = 0;
00526
00527 line = mtp_http_ParseHeader(http, string);
00528
00529
00530 if(
00531 http->content_type != NULL &&
00532 !strncmp(
00533 http->content_type,
00534 "multipart/mixed",
00535 strlen("multipart/mixed")
00536 )
00537 )
00538 {
00539 tmp = strstr(http->content_type, "boundary=");
00540 tmp += strlen("boundary=.");
00541 tmp2 = strchr(tmp, '\"');
00542 http->boundary = (char*)malloc(sizeof(char) * (tmp2 - tmp + 3));
00543
00544 http->boundary[0] = '-';
00545 http->boundary[1] = '-';
00546 for (i = 0; tmp != tmp2; i++, tmp++) {
00547 http->boundary[i+2] = *tmp;
00548 }
00549 http->boundary[i+2] = '\0';
00550
00551
00552 tmp = (char*)line;
00553 http->message_parts = 0;
00554 while((tmp = strstr(tmp, http->boundary))) {
00555 http->message_parts++;
00556 tmp++;
00557 }
00558 http->message_parts--;
00559 } else {
00560 http->boundary = NULL;
00561 http->message_parts = 1;
00562 }
00563
00564 if (http->message_parts == 1) {
00565 http->content = (struct mtp_http_content_s*)malloc(sizeof(struct mtp_http_content_s));
00566 memset(http->content, 0, sizeof(struct mtp_http_content_s));
00567
00568 if (line != NULL) {
00569 http->content->data = (void*)malloc
00570 (
00571 sizeof(char) *
00572 (strlen(line)+1)
00573 );
00574 strcpy((char*)http->content->data, line);
00575 if (http->content_type != NULL) {
00576 http->content->content_type = strdup(http->content_type);
00577 }
00578 }
00579 } else {
00580 http->content = (struct mtp_http_content_s*)malloc(
00581 sizeof(struct mtp_http_content_s) * http->message_parts );
00582 memset(http->content, 0, sizeof(struct mtp_http_content_s) * http->message_parts);
00583
00584 line = strstr(line, http->boundary);
00585 line += strlen(http->boundary);
00586 line = strchr(line, '\n');
00587 line++;
00588 for(i = 0; i < http->message_parts; i++) {
00589
00590
00591
00592
00593
00594
00595
00596 tmp = strstr(line + strlen(http->boundary), http->boundary);
00597
00598 do{
00599
00600
00601 line = http_GetExpression
00602 (
00603 line,
00604 &expr
00605 );
00606
00607 err_code = http_ParseExpression
00608 (
00609 expr,
00610 &name,
00611 &value
00612 );
00613 if
00614 (
00615 (name == NULL) ||
00616 (value == NULL)
00617 )
00618 {
00619 if (expr != NULL) {
00620 free(expr);
00621 }
00622 break;
00623 }
00624 if (!strcmp(name, "Content-Type")) {
00625 http->content[i].content_type = (char*)malloc(
00626 sizeof(char) * (strlen(value)+1));
00627 strcpy(http->content[i].content_type, value);
00628 }
00629
00630
00631 if (expr != NULL) {
00632 free(expr);
00633 expr = NULL;
00634 }
00635 if (name != NULL) {
00636 free(name);
00637 name = NULL;
00638 }
00639 if (value != NULL) {
00640 free(value);
00641 value = NULL;
00642 }
00643 } while(line != NULL && err_code == MC_SUCCESS);
00644
00645 http->content[i].data = (void*)malloc(tmp-line+sizeof(char));
00646 memcpy(http->content[i].data, line, tmp-line);
00647 ((char*)http->content[i].data)[tmp-line] = '\0';
00648
00649 line = tmp + strlen(http->boundary);
00650 line = strchr(line, '\n');
00651 line++;
00652 }
00653 }
00654 if (
00655 (http->http_performative == HTTP_POST) ||
00656 (http->http_performative == HTTP_PUT) ||
00657 (http->http_performative == HTTP_RESPONSE)
00658 )
00659 return 0;
00660 else
00661 return 1;
00662 }
00663
00664 const char* http_ParseRequest(
00665 mtp_http_p http,
00666 const char* string )
00667 {
00668 const char* cur;
00669 char* token;
00670 char* tmp = NULL;
00671 char* target;
00672 int len;
00673
00674 cur = string;
00675 cur = http_GetToken(cur, &token);
00676 if (token == NULL) {
00677 return NULL;
00678 }
00679 if (!strcmp(token, "GET")) {
00680 http->http_performative = HTTP_GET;
00681 cur = http_GetToken(cur, &tmp);
00682
00683 if(tmp) free(tmp);
00684 } else if(!strcmp(token, "HEAD")) {
00685
00686 http->http_performative = HTTP_HEAD;
00687 } else if(!strcmp(token, "POST")) {
00688 http->http_performative = HTTP_POST;
00689 cur = http_GetToken(cur, &tmp);
00690 if(tmp != NULL) {
00691 if(!strncmp(tmp, "http://",7)) {
00692 target = strchr(tmp+7, (int)'/');
00693 } else {
00694 target = strchr(tmp, (int)'/');
00695 }
00696 if (target == NULL)
00697 target = tmp;
00698 http->target = (char*) malloc(sizeof(char) * (strlen(target)+1));
00699 strcpy(http->target, target);
00700 free(tmp);
00701 }
00702 } else if(!strcmp(token, "PUT")) {
00703 http->http_performative = HTTP_PUT;
00704 cur = http_GetToken(cur, &tmp);
00705 if (tmp != NULL) {
00706 http->target = (char*)malloc(sizeof(char)*(strlen(tmp)+1));
00707 strcpy(http->target, tmp);
00708 free(tmp);
00709 }
00710 } else if(!strcmp(token, "DELETE")) {
00711 http->http_performative = HTTP_DELETE;
00712 } else if(!strcmp(token, "TRACE")) {
00713 http->http_performative = HTTP_TRACE;
00714 } else if(!strcmp(token, "OPTIONS")) {
00715 http->http_performative = HTTP_OPTIONS;
00716 } else if(!strcmp(token, "CONNECT")) {
00717 http->http_performative = HTTP_CONNECT;
00718 } else if(!strncmp(token, "HTTP/", 5)) {
00719
00720 http->http_performative = HTTP_RESPONSE;
00721
00722 free(token);
00723 cur = http_GetToken(cur, &token);
00724 sscanf(token, "%d", &http->response_code);
00725
00726 len = strstr(cur, "\r\n") - cur + 1;
00727 http->response_string = malloc(
00728 sizeof(char) * (len +1) );
00729 strncpy(
00730 http->response_string,
00731 cur,
00732 len );
00733 http->response_string[len] = '\0';
00734 } else {
00735
00736
00737 http->http_performative = HTTP_PERFORMATIVE_UNDEF;
00738 }
00739 free(token);
00740 cur = string;
00741 while(*cur != '\0') {
00742 if(*cur == '\n') {
00743 while (*cur == '\n' || *cur == '\r' || *cur == ' ')
00744 cur++;
00745 break;
00746 }
00747 cur++;
00748 }
00749 return cur;
00750 }
00751
00752 const char*
00753 http_GetToken(const char* string, char** token)
00754 {
00755 const char* cur;
00756 const char* begin;
00757 char* itr;
00758
00759 cur = string;
00760
00761 if (string[0] == '\r' && string[1] == '\n') {
00762 *token = NULL;
00763 return NULL;
00764 }
00765
00766 while(*cur == ' ' || *cur == '\t' || *cur == '\r' || *cur == '\n') cur++;
00767
00768 begin = cur;
00769 while(*cur != '\0') {
00770 cur++;
00771 if (*cur == ' ' || *cur == '\t' || *cur == '\r' || *cur == '\n')
00772 break;
00773 }
00774 cur--;
00775 *token = (char*)malloc(cur - begin + 4*sizeof(char));
00776 itr = *token;
00777 while (begin <= cur) {
00778 *itr = *begin;
00779 itr++;
00780 begin++;
00781 }
00782 *itr='\0';
00783 return cur+1;
00784 }
00785
00786 int mtp_http_ParseResponse(struct mtp_http_s* http, const char* string)
00787 {
00788
00789 }
00790
00791 int
00792 mtp_http_ComposeMessage(message_p message)
00793 {
00794 char* http_header;
00795 char* tmp;
00796 char buf[20];
00797 if (message->isHTTP) {
00798
00799 return 0;
00800 }
00801
00802 http_header = (char*)malloc
00803 (
00804 sizeof(char) * (1400 + strlen(message->to_address))
00805 );
00806 http_header[0] = '\0';
00807 strcat(http_header, "POST /");
00808 strcat(http_header, message->target);
00809 strcat(http_header, " HTTP/1.0\r\n");
00810 strcat(http_header, "User-Agent: MobileC/");
00811 strcat(http_header, PACKAGE_VERSION);
00812 strcat(http_header, "\r\n");
00813 strcat(http_header, "Host: ");
00814 strcat(http_header, message->to_address);
00815 strcat(http_header, "\r\n");
00816 strcat(http_header, "Content-Type: text/plain\r\n");
00817 strcat(http_header, "Connection: Close\r\n");
00818 strcat(http_header, "Content-Length: ");
00819 sprintf(buf, "%d", strlen(message->message_body) + 1);
00820 strcat(http_header, buf);
00821 strcat(http_header, "\r\n\r\n");
00822
00823 tmp = (char*)malloc
00824 (
00825 sizeof(char) *
00826 (strlen(http_header) + strlen(message->message_body) + 1)
00827 );
00828 tmp[0] = '\0';
00829 strcpy(tmp, http_header);
00830 strcat(tmp, message->message_body);
00831 free(message->message_body);
00832 message->message_body = tmp;
00833 free(http_header);
00834 return MC_SUCCESS;
00835 }
00836
00837 struct message_s*
00838 mtp_http_CreateMessage(
00839 mtp_http_t* mtp_http,
00840 char* hostname,
00841 int port)
00842 {
00843 int i;
00844 int num;
00845 char buf[30];
00846 char boundary[30];
00847 message_t* message;
00848 dynstring_t* http_header;
00849 dynstring_t* http_body;
00850 http_header = dynstring_New();
00851 http_body = dynstring_New();
00852 dynstring_Append(http_header, "POST /");
00853 dynstring_Append(http_header, mtp_http->target);
00854 dynstring_Append(http_header, " HTTP/1.1\r\n");
00855 dynstring_Append(http_header, "User-Agent: MobileC/");
00856 dynstring_Append(http_header, PACKAGE_VERSION);
00857 dynstring_Append(http_header, "\r\n");
00858 dynstring_Append(http_header, "Host: ");
00859 dynstring_Append(http_header, mtp_http->host);
00860 dynstring_Append(http_header, ":");
00861 sprintf(buf, "%d", port);
00862 dynstring_Append(http_header, buf);
00863 dynstring_Append(http_header, "\r\n");
00864 dynstring_Append(http_header, "Cache-Control: no-cache\r\n");
00865 dynstring_Append(http_header, "Mime-Version: 1.0\r\n");
00866
00867
00868
00869 if(mtp_http->message_parts == 1) {
00870 dynstring_Append(http_header, "Content-Type: text/plain\r\n");
00871 dynstring_Append(http_header, "Content-Length: ");
00872 sprintf(buf, "%d", strlen((char*)mtp_http->content[0].data));
00873 dynstring_Append(http_header, buf);
00874 dynstring_Append(http_header, "\r\n\r\n");
00875 dynstring_Append(http_header, (char*)mtp_http->content[0].data);
00876 } else {
00877
00878 srand(time(NULL));
00879 strcpy(boundary, "--");
00880 for(i = 2; i < 26; i++) {
00881 num = rand() % 36;
00882 if(num < 10) {
00883 boundary[i] = (char)(48 + num);
00884 } else {
00885 boundary[i] = (char)( (num-10)+65);
00886 }
00887 }
00888 boundary[i] = '\0';
00889
00890
00891
00892 dynstring_Append(http_body, "This is not part of the MIME multipart encoded message.\r\n");
00893 for(i = 0; i<mtp_http->message_parts; i++) {
00894 dynstring_Append(http_body, boundary);
00895 dynstring_Append(http_body, "\r\nContent-Type: ");
00896 dynstring_Append(http_body, mtp_http->content[i].content_type);
00897 dynstring_Append(http_body, "\r\n\r\n");
00898 dynstring_Append(http_body, (char*)mtp_http->content[i].data);
00899 dynstring_Append(http_body, "\r\n");
00900 }
00901
00902 dynstring_Append(http_body, boundary);
00903 dynstring_Append(http_body, "--");
00904 dynstring_Append(http_body, "\r\n\r\n");
00905
00906
00907 dynstring_Append(http_header, "Content-Length: ");
00908 sprintf(buf, "%d", http_body->len-2 );
00909 dynstring_Append(http_header, buf);
00910 dynstring_Append(http_header, "\r\n");
00911 dynstring_Append(http_header, "Content-Type: multipart/mixed ; boundary=\"");
00912 dynstring_Append(http_header, boundary+2);
00913 dynstring_Append(http_header, "\"\r\n");
00914
00915 }
00916 dynstring_Append(http_header, "Connection: Close\r\n\r\n");
00917 message = message_New();
00918 message->to_address = (char*)malloc(
00919 sizeof(char) * (strlen(hostname)+15) );
00920 sprintf(message->to_address, "%s:%d", hostname, port);
00921 message->message_body = (char*) malloc(
00922 sizeof(char) * (http_header->len + http_body->len + 1));
00923 strcpy(message->message_body, http_header->message);
00924 strcat(message->message_body, http_body->message);
00925 dynstring_Destroy(http_header);
00926 dynstring_Destroy(http_body);
00927 message->isHTTP = 1;
00928 return message;
00929 }
00930