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