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