From 8e48052dac5bca535f2b23b121dca88a861a9da2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20P=C3=ADsa=C5=99?= Date: Mon, 26 Apr 2010 22:13:40 +0200 Subject: [PATCH] Add isds_normalize_mime_type() public function This function reternus more usable MIME type than file name extension. Not all conversions are implementes because offical documentaition does not specify some docuemnt formats. --- doc/message | 2 ++ src/isds.c | 49 +++++++++++++++++++++++++++++ src/isds.h | 8 +++++ test/Makefile.am | 4 ++- test/normalize_mime_type.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 test/normalize_mime_type.c diff --git a/doc/message b/doc/message index d6d1021..450dcfb 100644 --- a/doc/message +++ b/doc/message @@ -4,6 +4,8 @@ Message specification Source: Provozní řád ISDS, version 2009-10-30, Page 13 Source: Webové služby ISDS pro manipulaci s datovými zprávami, version 2.6 (2010-01-18) [DataMessage_ws.pdf] +Source: Registration of media type FO and ZFO + Message ::= Envelope, Content diff --git a/src/isds.c b/src/isds.c index a3d6e4c..8c07897 100644 --- a/src/isds.c +++ b/src/isds.c @@ -20,6 +20,35 @@ const char isds_locator[] = "https://www.mojedatovaschranka.cz/"; /* Base URL of production ISDS instance */ const char isds_testing_locator[] = "https://www.czebox.cz/"; +/* Extension to MIME type map */ +static xmlChar *extension_map_mime[] = { + BAD_CAST "pdf", BAD_CAST "application/pdf", + BAD_CAST "xml", BAD_CAST "application/xml", + BAD_CAST "fo", BAD_CAST "application/vnd.software602.filler.xml+form", + BAD_CAST "zfo", BAD_CAST "application/vnd.software602.filler.xml+zip+form", + BAD_CAST "html", BAD_CAST "text/html", + BAD_CAST "htm", BAD_CAST "text/html", + BAD_CAST "odt", BAD_CAST "application/vnd.oasis.opendocument.text", + BAD_CAST "ods", BAD_CAST "application/vnd.oasis.opendocument.spreadsheet", + BAD_CAST "odp", BAD_CAST "application/vnd.oasis.opendocument.presentation", + BAD_CAST "txt", BAD_CAST "text/plain", + BAD_CAST "rtf", BAD_CAST "application/rtf", + BAD_CAST "doc", BAD_CAST "application/msword", + BAD_CAST "xls", BAD_CAST "application/vnd.ms-excel", + BAD_CAST "ppt", BAD_CAST "application/vnd.ms-powerpoint", + BAD_CAST "jpg", BAD_CAST "image/jpeg", + BAD_CAST "jpeg", BAD_CAST "image/jpeg", + BAD_CAST "jfif", BAD_CAST "image/jpeg", + BAD_CAST "png", BAD_CAST "image/png", + BAD_CAST "tiff", BAD_CAST "image/tiff", + BAD_CAST "gif", BAD_CAST "image/gif", + BAD_CAST "mpeg1", BAD_CAST "video/mpeg", + BAD_CAST "mpeg2", BAD_CAST "video/mpeg2", + BAD_CAST "wav", BAD_CAST "audio/x-wav", + BAD_CAST "mp2", BAD_CAST "audio/mpeg", + BAD_CAST "mp3", BAD_CAST "audio/mpeg", + /* TODO: Add MIME types for ISDOC, X.509 certificates, CMS and TST */ +}; /* Deallocate structure isds_pki_credentials and NULL it. * Passphrase is discarded. @@ -8830,6 +8859,26 @@ const struct isds_document *isds_find_document_by_id( } +/* Normalize @mime_type to be proper MIME type. + * ISDS servers passes invalid MIME types (e.g. "pdf"). This function tries to + * guess regular MIME type (e.g. "application/pdf"). + * @mime_type is UTF-8 encoded MIME type to fix + * @return original @mime_type if no better interpretation exists, or array to + * constant static UTF-8 encoded string with proper MIME type. */ +char *isds_normalize_mime_type(const char* mime_type) { + if (!mime_type) return NULL; + + for (int offset = 0; + offset < sizeof(extension_map_mime)/sizeof(extension_map_mime[0]); + offset += 2) { + if (!xmlStrcmp((const xmlChar*) mime_type, extension_map_mime[offset])) + return (char *) extension_map_mime[offset + 1]; + } + + return (char *) mime_type; +} + + /*int isds_get_message(struct isds_ctx *context, const unsigned int id, struct isds_message **message); int isds_send_message(struct isds_ctx *context, struct isds_message *message); diff --git a/src/isds.h b/src/isds.h index 179b2cd..73ce5ab 100644 --- a/src/isds.h +++ b/src/isds.h @@ -1210,6 +1210,14 @@ isds_error isds_request_new_testing_box(struct isds_ctx *context, const struct isds_document *isds_find_document_by_id( const struct isds_list *documents, const char *id); +/* Normalize @mime_type to be proper MIME type. + * ISDS servers passes invalid MIME types (e.g. "pdf"). This function tries to + * guess regular MIME type (e.g. "application/pdf"). + * @mime_type is UTF-8 encoded MIME type to fix + * @return original @mime_type if no better interpretation exists, or array to + * constant static UTF-8 encoded string with proper MIME type. */ +char *isds_normalize_mime_type(const char* mime_type); + /* Free isds_list with all member data. * @list list to free, on return will be NULL */ void isds_list_free(struct isds_list **list); diff --git a/test/Makefile.am b/test/Makefile.am index 0b14f3c..8a98c14 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -14,6 +14,7 @@ LDADD = @XML_LIBS@ @LIBCURL@ @LIBGCRYPT_LIBS@ @GPGME_LIBS@ @EXPAT_LIBS@ \ @LTLIBINTL@ TESTS = compute_hash context guess_raw_type init_gpgpme isds_init load_raw \ + normalize_mime_type \ isds-dbtype isds-filemetatype isds-hash_algorithm @@ -64,7 +65,8 @@ context_SOURCES = context.c $(common) guess_raw_type_SOURCES = guess_raw_type.c $(common) init_gpgpme_SOURCES = init_gpgpme.c $(common) isds_init_SOURCES = isds_init.c $(common) -load_raw_SOURCES = load_raw.c $(common) +load_raw_SOURCES = load_raw.c $(common) +normalize_mime_type_SOURCES = normalize_mime_type.c $(common) # Access static symbols from isds.c isds_dbtype_SOURCES = isds-dbtype.c $(isds_common) diff --git a/test/normalize_mime_type.c b/test/normalize_mime_type.c new file mode 100644 index 0000000..748aa27 --- /dev/null +++ b/test/normalize_mime_type.c @@ -0,0 +1,77 @@ +#include "test.h" +#include "crypto.h" +#include + + +static int test_normalize(const char *input, const char *expected) { + const char *output = isds_normalize_mime_type(input); + + if (!expected) { + if (!output) { + PASS_TEST; + } else { + FAIL_TEST("isds_normalize_mime_type(%s) returned=%s, expected=%s", + input, output, expected); + } + } else if (!output) { + FAIL_TEST("isds_normalize_mime_type(%s) returned=%s, expected=%s", + input, output, expected); + } + + if (strcmp(expected, output)) { + FAIL_TEST("isds_normalize_mime_type(%s) returned=%s, expected=%s", + input, output, expected); + } + + PASS_TEST; +} + + +int main(int argc, char **argv) { + TEST("NULL is idempotent", test_normalize, NULL, NULL); + TEST("X-Invalid is idempotent", test_normalize, "X-Invalid", "X-Invalid"); + TEST("pdf", test_normalize, "pdf", "application/pdf"); + TEST("xml", test_normalize, "xml", "application/xml"); + TEST("fo", test_normalize, "fo", + "application/vnd.software602.filler.xml+form"); + TEST("zfo", test_normalize, "zfo", + "application/vnd.software602.filler.xml+zip+form"); + TEST("html", test_normalize, "html", "text/html"); + TEST("htm", test_normalize, "htm", "text/html"); + TEST("odt", test_normalize, "odt", + "application/vnd.oasis.opendocument.text"); + TEST("ods", test_normalize, "ods", + "application/vnd.oasis.opendocument.spreadsheet"); + TEST("odp", test_normalize, "odp", + "application/vnd.oasis.opendocument.presentation"); + TEST("txt", test_normalize, "txt", "text/plain"); + TEST("rtf", test_normalize, "rtf", "application/rtf"); + TEST("doc", test_normalize, "doc", "application/msword"); + TEST("xls", test_normalize, "xls", "application/vnd.ms-excel"); + TEST("ppt", test_normalize, "ppt", "application/vnd.ms-powerpoint"); + TEST("jpg", test_normalize, "jpg", "image/jpeg"); + TEST("jpeg", test_normalize, "jpeg", "image/jpeg"); + TEST("jfif", test_normalize, "jfif", "image/jpeg"); + TEST("png", test_normalize, "png", "image/png"); + TEST("tiff", test_normalize, "tiff", "image/tiff"); + TEST("gif", test_normalize, "gif", "image/gif"); + TEST("mpeg1", test_normalize, "mpeg1", "video/mpeg"); + TEST("mpeg2", test_normalize, "mpeg2", "video/mpeg2"); + TEST("wav", test_normalize, "wav", "audio/x-wav"); + TEST("mp2", test_normalize, "mp2", "audio/mpeg"); + TEST("mp3", test_normalize, "mp3", "audio/mpeg"); + /*TEST("isdoc", test_normalize, "isdoc", ""); + TEST("isdocx", test_normalize, "isdocx", ""); + TEST("cer", test_normalize, "cer", ""); + TEST("crt", test_normalize, "crt", ""); + TEST("der", test_normalize, "der", ""); + TEST("pk7", test_normalize, "pk7", ""); + TEST("p7b", test_normalize, "p7b", ""); + TEST("p7c", test_normalize, "p7c", ""); + TEST("p7f", test_normalize, "p7f", ""); + TEST("p7m", test_normalize, "p7m", ""); + TEST("p7s", test_normalize, "p7s", ""); + TEST("tst", test_normalize, "tst", "");*/ + + SUM_TEST(); +} -- 2.11.4.GIT