Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
mime.h File Reference
#include "gwlib/gwlib.h"

Go to the source code of this file.

Typedefs

typedef struct MIMEEntity MIMEEntity
 

Functions

MIMEEntitymime_entity_create (void)
 
void mime_entity_destroy (MIMEEntity *e)
 
MIMEEntitymime_entity_duplicate (MIMEEntity *e)
 
void mime_replace_headers (MIMEEntity *e, List *headers)
 
int mime_entity_num_parts (MIMEEntity *e)
 
void mime_entity_add_part (MIMEEntity *e, MIMEEntity *part)
 
MIMEEntitymime_entity_get_part (MIMEEntity *e, int i)
 
void mime_entity_replace_part (MIMEEntity *e, int i, MIMEEntity *newpart)
 
void mime_entity_remove_part (MIMEEntity *e, int i)
 
void mime_entity_set_body (MIMEEntity *e, Octstr *body)
 
MIMEEntitymime_multipart_start_elem (MIMEEntity *e)
 
MIMEEntitymime_octstr_to_entity (Octstr *mime)
 
MIMEEntitymime_http_to_entity (List *headers, Octstr *body)
 
Octstrmime_entity_to_octstr (MIMEEntity *m)
 
Listmime_entity_headers (MIMEEntity *m)
 
Octstrmime_entity_body (MIMEEntity *m)
 
void mime_entity_dump (MIMEEntity *m)
 

Typedef Documentation

typedef struct MIMEEntity MIMEEntity

Definition at line 96 of file mime.h.

Function Documentation

void mime_entity_add_part ( MIMEEntity e,
MIMEEntity part 
)

Definition at line 560 of file mime.c.

References gw_assert(), gwlist_append(), mime_entity_duplicate(), and MIMEEntity::multiparts.

561 {
562  gw_assert(e != NULL);
563  gw_assert(part != NULL);
564 
566 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
List * multiparts
Definition: mime.c:78
gw_assert(wtls_machine->packet_to_send!=NULL)
MIMEEntity * mime_entity_duplicate(MIMEEntity *e)
Definition: mime.c:521
Octstr* mime_entity_body ( MIMEEntity m)

Definition at line 485 of file mime.c.

References MIMEEntity::body, debug(), gw_assert(), MIMEEntity::headers, mime_entity_create(), mime_entity_destroy(), mime_entity_num_parts(), mime_entity_to_octstr(), octstr_destroy(), octstr_dump, octstr_duplicate, parse_context_create(), parse_context_destroy(), parse_get_rest(), and read_mime_headers().

486 {
487  Octstr *os, *body;
489  MIMEEntity *e;
490 
491  gw_assert(m != NULL && m->headers != NULL);
492 
493  /* For non-multipart, return body directly. */
494  if (mime_entity_num_parts(m) == 0)
495  return octstr_duplicate(m->body);
496 
497  os = mime_entity_to_octstr(m);
498  context = parse_context_create(os);
499  e = mime_entity_create();
500 
501  /* parse the headers up to the body */
502  if ((read_mime_headers(context, e->headers) != 0) || e->headers == NULL) {
503  debug("mime.parse",0,"Failed to read MIME headers in Octstr block:");
504  octstr_dump(os, 0);
506  parse_context_destroy(context);
507  return NULL;
508  }
509 
510  /* the rest is the body */
511  body = parse_get_rest(context);
512 
513  octstr_destroy(os);
515  parse_context_destroy(context);
516 
517  return body;
518 }
Definition: parse.c:65
Octstr * body
Definition: mime.c:79
MIMEEntity * mime_entity_create(void)
Definition: mime.c:87
Octstr * mime_entity_to_octstr(MIMEEntity *m)
Definition: mime.c:216
void mime_entity_destroy(MIMEEntity *e)
Definition: mime.c:105
#define octstr_duplicate(ostr)
Definition: octstr.h:187
#define octstr_dump(ostr, level,...)
Definition: octstr.h:564
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
gw_assert(wtls_machine->packet_to_send!=NULL)
static int read_mime_headers(ParseContext *context, List *headers)
Definition: mime.c:129
void parse_context_destroy(ParseContext *context)
Definition: parse.c:88
Definition: octstr.c:118
List * headers
Definition: mime.c:77
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
Octstr * parse_get_rest(ParseContext *context)
Definition: parse.c:329
ParseContext * parse_context_create(Octstr *str)
Definition: parse.c:74
int mime_entity_num_parts(MIMEEntity *e)
Definition: mime.c:550
MIMEEntity* mime_entity_create ( void  )

Definition at line 87 of file mime.c.

References MIMEEntity::body, gwlist_create, MIMEEntity::headers, http_create_empty_headers(), MIMEEntity::multiparts, and MIMEEntity::start.

Referenced by mime_entity_body(), mime_entity_duplicate(), and mime_something_to_entity().

88 {
89  MIMEEntity *e;
90 
91  e = gw_malloc(sizeof(MIMEEntity));
94  e->body = NULL;
95  e->start = NULL;
96 
97  return e;
98 }
Octstr * body
Definition: mime.c:79
List * multiparts
Definition: mime.c:78
List * http_create_empty_headers(void)
Definition: http.c:2849
struct MIMEEntity * start
Definition: mime.c:80
List * headers
Definition: mime.c:77
#define gwlist_create()
Definition: list.h:136
void mime_entity_destroy ( MIMEEntity e)

Definition at line 105 of file mime.c.

References MIMEEntity::body, gw_assert(), gwlist_destroy(), MIMEEntity::headers, mime_entity_destroy_item(), MIMEEntity::multiparts, octstr_destroy(), octstr_destroy_item(), and MIMEEntity::start.

Referenced by main(), mime_entity_body(), mime_entity_destroy_item(), mime_entity_remove_part(), mime_entity_replace_part(), and mime_something_to_entity().

106 {
107  gw_assert(e != NULL);
108 
109  if (e->headers != NULL)
111  if (e->multiparts != NULL)
113  octstr_destroy(e->body);
114  e->start = NULL; /* will be destroyed on it's own via gwlist_destroy */
115 
116  gw_free(e);
117 }
Octstr * body
Definition: mime.c:79
List * multiparts
Definition: mime.c:78
static void mime_entity_destroy_item(void *e)
Definition: mime.c:100
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
void octstr_destroy_item(void *os)
Definition: octstr.c:334
gw_assert(wtls_machine->packet_to_send!=NULL)
struct MIMEEntity * start
Definition: mime.c:80
List * headers
Definition: mime.c:77
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
void mime_entity_dump ( MIMEEntity m)

Definition at line 717 of file mime.c.

References debug(), gw_assert(), MIMEEntity::headers, and mime_entity_dump_real().

Referenced by main().

718 {
719  gw_assert(m != NULL && m->headers != NULL);
720 
721  debug("mms",0,"Dumping MIMEEntity at address %p", m);
722  mime_entity_dump_real(m, 0);
723 }
static void mime_entity_dump_real(MIMEEntity *m, unsigned int level)
Definition: mime.c:679
gw_assert(wtls_machine->packet_to_send!=NULL)
List * headers
Definition: mime.c:77
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
MIMEEntity* mime_entity_duplicate ( MIMEEntity e)

Definition at line 521 of file mime.c.

References MIMEEntity::body, gwlist_append(), gwlist_get(), gwlist_len(), MIMEEntity::headers, mime_entity_create(), mime_entity_duplicate(), mime_replace_headers(), MIMEEntity::multiparts, and octstr_duplicate.

Referenced by mime_entity_add_part(), mime_entity_duplicate(), mime_entity_get_part(), mime_entity_replace_part(), and mime_multipart_start_elem().

522 {
523  MIMEEntity *copy = mime_entity_create();
524  int i, n;
525 
526  mime_replace_headers(copy, e->headers);
527  copy->body = e->body ? octstr_duplicate(e->body) : NULL;
528 
529  for (i = 0, n = gwlist_len(e->multiparts); i < n; i++)
530  gwlist_append(copy->multiparts,
532  return copy;
533 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
Octstr * body
Definition: mime.c:79
List * multiparts
Definition: mime.c:78
MIMEEntity * mime_entity_create(void)
Definition: mime.c:87
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void mime_replace_headers(MIMEEntity *e, List *headers)
Definition: mime.c:536
List * headers
Definition: mime.c:77
MIMEEntity * mime_entity_duplicate(MIMEEntity *e)
Definition: mime.c:521
MIMEEntity* mime_entity_get_part ( MIMEEntity e,
int  i 
)

Definition at line 570 of file mime.c.

References gw_assert(), gwlist_get(), gwlist_len(), mime_entity_duplicate(), and MIMEEntity::multiparts.

571 {
572  MIMEEntity *m;
573  gw_assert(e != NULL);
574  gw_assert(i >= 0);
576 
577  m = gwlist_get(e->multiparts, i);
578  gw_assert(m);
579  return mime_entity_duplicate(m);
580 }
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
List * multiparts
Definition: mime.c:78
gw_assert(wtls_machine->packet_to_send!=NULL)
MIMEEntity * mime_entity_duplicate(MIMEEntity *e)
Definition: mime.c:521
List* mime_entity_headers ( MIMEEntity m)

Definition at line 469 of file mime.c.

References fix_boundary_element(), gw_assert(), MIMEEntity::headers, http_header_duplicate(), and mime_entity_num_parts().

470 {
471  List *headers;
472 
473  gw_assert(m != NULL && m->headers != NULL);
474 
475  /* Need a fixup before hand over. */
476  if (mime_entity_num_parts(m) > 0)
477  fix_boundary_element(m->headers, NULL);
478 
479  headers = http_header_duplicate(m->headers);
480 
481  return headers;
482 }
static void fix_boundary_element(List *headers, Octstr **boundary_elem)
Definition: mime.c:163
gw_assert(wtls_machine->packet_to_send!=NULL)
List * headers
Definition: mime.c:77
List * http_header_duplicate(List *headers)
Definition: http.c:2946
Definition: list.c:102
int mime_entity_num_parts(MIMEEntity *e)
Definition: mime.c:550
int mime_entity_num_parts ( MIMEEntity e)

Definition at line 550 of file mime.c.

References gw_assert(), gwlist_len(), and MIMEEntity::multiparts.

Referenced by mime_entity_body(), and mime_entity_headers().

551 {
552  gw_assert(e != NULL);
553  return e->multiparts ? gwlist_len(e->multiparts) : 0;
554 }
long gwlist_len(List *list)
Definition: list.c:166
List * multiparts
Definition: mime.c:78
gw_assert(wtls_machine->packet_to_send!=NULL)
void mime_entity_remove_part ( MIMEEntity e,
int  i 
)

Definition at line 584 of file mime.c.

References gw_assert(), gwlist_delete(), gwlist_get(), gwlist_len(), mime_entity_destroy(), MIMEEntity::multiparts, and MIMEEntity::start.

585 {
586  MIMEEntity *m;
587 
588  gw_assert(e != NULL);
589  gw_assert(i >= 0);
591 
592 
593  m = gwlist_get(e->multiparts, i);
594  gwlist_delete(e->multiparts, i, 1);
595  if (m == e->start) e->start = NULL;
596 
598 }
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
List * multiparts
Definition: mime.c:78
void gwlist_delete(List *list, long pos, long count)
Definition: list.c:232
void mime_entity_destroy(MIMEEntity *e)
Definition: mime.c:105
gw_assert(wtls_machine->packet_to_send!=NULL)
struct MIMEEntity * start
Definition: mime.c:80
void mime_entity_replace_part ( MIMEEntity e,
int  i,
MIMEEntity newpart 
)

Definition at line 601 of file mime.c.

References gw_assert(), gwlist_delete(), gwlist_get(), gwlist_insert(), gwlist_len(), mime_entity_destroy(), mime_entity_duplicate(), MIMEEntity::multiparts, and MIMEEntity::start.

602 {
603 
604  MIMEEntity *m;
605 
606  gw_assert(e != NULL);
607  gw_assert(i >= 0);
609 
610  m = gwlist_get(e->multiparts, i);
611  gwlist_delete(e->multiparts, i, 1);
613  if (m == e->start) e->start = NULL;
614 
616 }
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
List * multiparts
Definition: mime.c:78
void gwlist_delete(List *list, long pos, long count)
Definition: list.c:232
void mime_entity_destroy(MIMEEntity *e)
Definition: mime.c:105
gw_assert(wtls_machine->packet_to_send!=NULL)
void gwlist_insert(List *list, long pos, void *item)
Definition: list.c:214
struct MIMEEntity * start
Definition: mime.c:80
MIMEEntity * mime_entity_duplicate(MIMEEntity *e)
Definition: mime.c:521
void mime_entity_set_body ( MIMEEntity e,
Octstr body 
)

Definition at line 622 of file mime.c.

References MIMEEntity::body, gw_assert(), octstr_destroy(), and octstr_duplicate.

623 {
624  gw_assert(e != NULL);
625  gw_assert(body != NULL);
626 
627  if (e->body)
628  octstr_destroy(e->body);
629  e->body = octstr_duplicate(body);
630 }
Octstr * body
Definition: mime.c:79
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
gw_assert(wtls_machine->packet_to_send!=NULL)
Octstr* mime_entity_to_octstr ( MIMEEntity m)

Definition at line 216 of file mime.c.

References MIMEEntity::body, boundary, finished, fix_boundary_element(), gw_assert(), gwlist_get(), gwlist_len(), MIMEEntity::headers, http_destroy_headers(), http_header_duplicate(), mime_entity_to_octstr(), MIMEEntity::multiparts, octstr_append(), octstr_create, octstr_destroy(), and octstr_imm().

Referenced by main(), mime_entity_body(), and mime_entity_to_octstr().

217 {
218  Octstr *mime, *boundary = NULL;
219  List *headers;
220  long i;
221 
222  gw_assert(m != NULL && m->headers != NULL);
223 
224  mime = octstr_create("");
225 
226  /*
227  * First of all check if we have further MIME entity dependencies,
228  * which means we have further MIMEEntities in our m->multiparts
229  * list. If no, then add headers and body and return. This is the
230  * easy case. Otherwise we have to loop inside our entities.
231  */
232  if (gwlist_len(m->multiparts) == 0) {
233  for (i = 0; i < gwlist_len(m->headers); i++) {
234  octstr_append(mime, gwlist_get(m->headers, i));
235  octstr_append(mime, octstr_imm("\r\n"));
236  }
237  octstr_append(mime, octstr_imm("\r\n"));
238  if (m->body != NULL)
239  octstr_append(mime, m->body);
240  goto finished;
241  }
242 
243  /* This call ensures boundary exists, and returns it */
244  fix_boundary_element(m->headers, &boundary);
245  headers = http_header_duplicate(m->headers);
246 
247  /* headers */
248  for (i = 0; i < gwlist_len(headers); i++) {
249  octstr_append(mime, gwlist_get(headers, i));
250  octstr_append(mime, octstr_imm("\r\n"));
251  }
252  http_destroy_headers(headers);
253  octstr_append(mime, octstr_imm("\r\n")); /* Mark end of headers. */
254 
255  /* loop through all MIME multipart entities of this entity */
256  for (i = 0; i < gwlist_len(m->multiparts); i++) {
257  MIMEEntity *e = gwlist_get(m->multiparts, i);
258  Octstr *body;
259 
260  octstr_append(mime, octstr_imm("\r\n--"));
261  octstr_append(mime, boundary);
262  octstr_append(mime, octstr_imm("\r\n"));
263 
264  /* call ourself to produce the MIME entity body */
265  body = mime_entity_to_octstr(e);
266  octstr_append(mime, body);
267 
268  octstr_destroy(body);
269  }
270 
271  octstr_append(mime, octstr_imm("\r\n--"));
272  octstr_append(mime, boundary);
273  octstr_append(mime, octstr_imm("--\r\n"));
274 
275  octstr_destroy(boundary);
276 
277 finished:
278 
279  return mime;
280 }
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
Octstr * body
Definition: mime.c:79
static char * boundary
Definition: test_ppg.c:97
List * multiparts
Definition: mime.c:78
static void fix_boundary_element(List *headers, Octstr **boundary_elem)
Definition: mime.c:163
void http_destroy_headers(List *headers)
Definition: http.c:2856
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
Octstr * mime_entity_to_octstr(MIMEEntity *m)
Definition: mime.c:216
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
gw_assert(wtls_machine->packet_to_send!=NULL)
Definition: octstr.c:118
List * headers
Definition: mime.c:77
List * http_header_duplicate(List *headers)
Definition: http.c:2946
Definition: list.c:102
MIMEEntity* mime_http_to_entity ( List headers,
Octstr body 
)

Definition at line 461 of file mime.c.

References gw_assert(), and mime_something_to_entity().

462 {
463  gw_assert(headers != NULL && body != NULL);
464 
465  return mime_something_to_entity(body, headers);
466 }
static MIMEEntity * mime_something_to_entity(Octstr *mime, List *headers)
Definition: mime.c:332
gw_assert(wtls_machine->packet_to_send!=NULL)
MIMEEntity* mime_multipart_start_elem ( MIMEEntity e)

Definition at line 633 of file mime.c.

References cid_matches(), get_start_param(), gw_assert(), gwlist_get(), gwlist_len(), MIMEEntity::headers, http_header_value(), mime_entity_duplicate(), MIMEEntity::multiparts, octstr_destroy(), octstr_imm(), MIMEEntity::start, and start.

634 {
635  gw_assert(e != NULL);
636 
637  /* If e->start element is not yet set, set it as follows:
638  * - if content type is not set, then set it to NULL
639  * - if the start element is not set but this is a multipart object, set
640  * it to first multipart element, else set it to null
641  * - if the start element of the content type is set, find a matching object
642  * and set e->start accordingly.
643  * Finally, return a copy of it.
644  */
645  if (!e->start) {
646  Octstr *ctype = http_header_value(e->headers, octstr_imm("Content-Type"));
647  Octstr *start = get_start_param(ctype);
648  int i;
649 
650  if (!ctype)
651  e->start = NULL;
652  else if (!start) {
653  if (gwlist_len(e->multiparts) > 0)
654  e->start = gwlist_get(e->multiparts, 0);
655  else
656  e->start = NULL;
657  } else
658  for (i = 0; i < gwlist_len(e->multiparts); i++) {
659  MIMEEntity *x = gwlist_get(e->multiparts, i);
660  if (cid_matches(x->headers, start)) {
661  e->start = x;
662  break;
663  }
664  }
665 
666  if (ctype)
667  octstr_destroy(ctype);
668  if (start)
669  octstr_destroy(start);
670  }
671 
672  return (e->start) ? mime_entity_duplicate(e->start) : NULL;
673 }
static Octstr * get_start_param(Octstr *content_type)
Definition: mime.c:282
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
List * multiparts
Definition: mime.c:78
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
static int cid_matches(List *headers, Octstr *start)
Definition: mime.c:300
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
gw_assert(wtls_machine->packet_to_send!=NULL)
Octstr * http_header_value(List *headers, Octstr *name)
Definition: http.c:2909
Definition: octstr.c:118
struct MIMEEntity * start
Definition: mime.c:80
List * headers
Definition: mime.c:77
MIMEEntity * mime_entity_duplicate(MIMEEntity *e)
Definition: mime.c:521
static int start
MIMEEntity* mime_octstr_to_entity ( Octstr mime)

Definition at line 453 of file mime.c.

References gw_assert(), and mime_something_to_entity().

Referenced by main(), and mime_something_to_entity().

454 {
455  gw_assert(mime != NULL);
456 
457  return mime_something_to_entity(mime, NULL);
458 }
static MIMEEntity * mime_something_to_entity(Octstr *mime, List *headers)
Definition: mime.c:332
gw_assert(wtls_machine->packet_to_send!=NULL)
void mime_replace_headers ( MIMEEntity e,
List headers 
)

Definition at line 536 of file mime.c.

References gw_assert(), MIMEEntity::headers, http_destroy_headers(), http_header_duplicate(), and MIMEEntity::start.

Referenced by mime_entity_duplicate().

537 {
538  gw_assert(e != NULL);
539  gw_assert(headers != NULL);
540 
542  e->headers = http_header_duplicate(headers);
543  e->start = NULL; /* clear it, since header change means it could have changed.*/
544 }
void http_destroy_headers(List *headers)
Definition: http.c:2856
gw_assert(wtls_machine->packet_to_send!=NULL)
struct MIMEEntity * start
Definition: mime.c:80
List * headers
Definition: mime.c:77
List * http_header_duplicate(List *headers)
Definition: http.c:2946
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.