Commit 347d04fe authored by Slava Monich's avatar Slava Monich

Merge pull request #44 from monich/content-type-name

Added Name parameter to the part's Content-Type header
parents 496ea36d 198ea973
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
MMS_LOG_MODULE_DEFINE("mms-attachment"); MMS_LOG_MODULE_DEFINE("mms-attachment");
#define MMS_ATTACHMENT_DEFAULT_TYPE "application/octet-stream" #define MMS_ATTACHMENT_DEFAULT_TYPE "application/octet-stream"
#define CONTENT_TYPE_PARAM_NAME "name"
G_DEFINE_TYPE(MMSAttachment, mms_attachment, G_TYPE_OBJECT); G_DEFINE_TYPE(MMSAttachment, mms_attachment, G_TYPE_OBJECT);
...@@ -250,6 +251,8 @@ mms_attachment_new( ...@@ -250,6 +251,8 @@ mms_attachment_new(
GMappedFile* map = g_mapped_file_new(path, FALSE, error); GMappedFile* map = g_mapped_file_new(path, FALSE, error);
if (map) { if (map) {
unsigned int flags = 0; unsigned int flags = 0;
char* media_type = NULL;
char* name = g_path_get_basename(path);
char* content_type = NULL; char* content_type = NULL;
GType type; GType type;
MMSAttachment* at; MMSAttachment* at;
...@@ -257,43 +260,61 @@ mms_attachment_new( ...@@ -257,43 +260,61 @@ mms_attachment_new(
if (info->content_type && info->content_type[0]) { if (info->content_type && info->content_type[0]) {
char** ct = mms_parse_http_content_type(info->content_type); char** ct = mms_parse_http_content_type(info->content_type);
if (ct) { if (ct) {
content_type = mms_unparse_http_content_type(ct); char** ptr;
gboolean append_name = TRUE;
if (!strcmp(ct[0], SMIL_CONTENT_TYPE)) { if (!strcmp(ct[0], SMIL_CONTENT_TYPE)) {
flags |= MMS_ATTACHMENT_SMIL; flags |= MMS_ATTACHMENT_SMIL;
} }
for (ptr = ct+1; *ptr; ptr+=2) {
if (!g_ascii_strcasecmp(ptr[0], CONTENT_TYPE_PARAM_NAME)) {
g_free(ptr[1]);
ptr[1] = g_strdup(name);
append_name = FALSE;
}
}
if (append_name) {
const guint len = g_strv_length(ct);
ct = g_renew(gchar*, ct, len+3);
ct[len] = g_strdup(CONTENT_TYPE_PARAM_NAME);
ct[len+1] = g_strdup(name);
ct[len+2] = NULL;
}
content_type = mms_unparse_http_content_type(ct);
media_type = g_strdup(ct[0]);
g_strfreev(ct); g_strfreev(ct);
} }
} }
if (!content_type) { if (!content_type) {
char* detected_type = mms_attachment_guess_content_type(path);
const char* default_charset = "utf-8"; const char* default_charset = "utf-8";
const char* charset = NULL; const char* charset = NULL;
const char* ct[4]; const char* ct[6];
int n = 0; int n = 0;
if (!strcmp(detected_type, SMIL_CONTENT_TYPE)) { media_type = mms_attachment_guess_content_type(path);
if (!strcmp(media_type, SMIL_CONTENT_TYPE)) {
flags |= MMS_ATTACHMENT_SMIL; flags |= MMS_ATTACHMENT_SMIL;
charset = default_charset; charset = default_charset;
} else if (g_str_has_prefix(detected_type, "text/")) { } else if (g_str_has_prefix(media_type, "text/")) {
charset = default_charset; charset = default_charset;
} }
ct[n++] = detected_type; ct[n++] = media_type;
if (charset) { if (charset) {
ct[n++] = "charset"; ct[n++] = "charset";
ct[n++] = charset; ct[n++] = charset;
} }
ct[n++] = CONTENT_TYPE_PARAM_NAME;
ct[n++] = name;
ct[n++] = NULL; ct[n++] = NULL;
content_type = mms_unparse_http_content_type((char**)ct); content_type = mms_unparse_http_content_type((char**)ct);
g_free(detected_type);
} }
MMS_DEBUG("%s: %s", path, content_type); MMS_DEBUG("%s: %s", path, content_type);
if (!strcmp(content_type, "image/jpeg")) { if (!strcmp(media_type, "image/jpeg")) {
type = MMS_TYPE_ATTACHMENT_JPEG; type = MMS_TYPE_ATTACHMENT_JPEG;
} else if (g_str_has_prefix(content_type, "image/")) { } else if (g_str_has_prefix(media_type, "image/")) {
type = MMS_TYPE_ATTACHMENT_IMAGE; type = MMS_TYPE_ATTACHMENT_IMAGE;
} else { } else {
type = MMS_TYPE_ATTACHMENT; type = MMS_TYPE_ATTACHMENT;
...@@ -305,10 +326,12 @@ mms_attachment_new( ...@@ -305,10 +326,12 @@ mms_attachment_new(
at->flags |= flags; at->flags |= flags;
at->file_name = at->original_file = path; at->file_name = at->original_file = path;
at->content_type = content_type; at->content_type = content_type;
at->content_location = g_path_get_basename(path); at->content_location = name;
at->content_id = (info->content_id && info->content_id[0]) ? at->content_id = (info->content_id && info->content_id[0]) ?
g_strdup(info->content_id) : g_strdup(info->content_id) :
g_strdup(at->content_location); g_strdup(at->content_location);
g_free(media_type);
return at; return at;
} }
g_free(path); g_free(path);
......
...@@ -32,10 +32,8 @@ ...@@ -32,10 +32,8 @@
# define ssize_t int # define ssize_t int
# define write _write # define write _write
# include <io.h> # include <io.h>
# define uninitialized_var(x) x
#else #else
# include <unistd.h> # include <unistd.h>
# define uninitialized_var(x) x = x
#endif #endif
#include <glib.h> #include <glib.h>
...@@ -1853,6 +1851,25 @@ static gboolean encode_quoted_string(struct file_buffer *fb, ...@@ -1853,6 +1851,25 @@ static gboolean encode_quoted_string(struct file_buffer *fb,
{ {
char *ptr; char *ptr;
char **text = user; char **text = user;
if (!*text)
return TRUE;
ptr = fb_request_field(fb, header, strlen(*text) + 2);
if (ptr == NULL)
return FALSE;
ptr[0] = '"';
strcpy(ptr + 1, *text);
return TRUE;
}
static gboolean encode_content_id(struct file_buffer *fb,
enum mms_header header, void *user)
{
char *ptr;
char **text = user;
unsigned int len; unsigned int len;
if (!*text) if (!*text)
...@@ -2067,7 +2084,7 @@ static gboolean mms_encode_send_req_part_header(struct mms_attachment *part, ...@@ -2067,7 +2084,7 @@ static gboolean mms_encode_send_req_part_header(struct mms_attachment *part,
unsigned int ct_len; unsigned int ct_len;
unsigned int cs_len; unsigned int cs_len;
const char *ct_str; const char *ct_str;
const char *uninitialized_var(cs_str); char *name = NULL;
unsigned int ctp_len; unsigned int ctp_len;
unsigned int cid_len; unsigned int cid_len;
unsigned int cloc_len; unsigned int cloc_len;
...@@ -2099,7 +2116,7 @@ static gboolean mms_encode_send_req_part_header(struct mms_attachment *part, ...@@ -2099,7 +2116,7 @@ static gboolean mms_encode_send_req_part_header(struct mms_attachment *part,
const char *key = parsed[i]; const char *key = parsed[i];
if (g_ascii_strcasecmp("charset", key) == 0) { if (g_ascii_strcasecmp("charset", key) == 0) {
cs_str = parsed[i+1]; const char *cs_str = parsed[i+1];
len += 1; len += 1;
...@@ -2111,6 +2128,10 @@ static gboolean mms_encode_send_req_part_header(struct mms_attachment *part, ...@@ -2111,6 +2128,10 @@ static gboolean mms_encode_send_req_part_header(struct mms_attachment *part,
goto done; goto done;
len += cs_len; len += cs_len;
} else if (g_ascii_strcasecmp("name", key) == 0) {
name = parsed[i+1];
len += 2 + strlen(name) + 1;
} }
} }
...@@ -2179,9 +2200,16 @@ static gboolean mms_encode_send_req_part_header(struct mms_attachment *part, ...@@ -2179,9 +2200,16 @@ static gboolean mms_encode_send_req_part_header(struct mms_attachment *part,
memcpy(ptr, &cs_val, cs_len); memcpy(ptr, &cs_val, cs_len);
} }
/* Encode "name" param */
if (name) {
if (encode_quoted_string(fb, WSP_PARAMETER_TYPE_NAME_DEFUNCT,
&name) == FALSE)
goto done;
}
/* Encode content-id */ /* Encode content-id */
if (part->content_id != NULL) { if (part->content_id != NULL) {
if (encode_quoted_string(fb, MMS_PART_HEADER_CONTENT_ID, if (encode_content_id(fb, MMS_PART_HEADER_CONTENT_ID,
&part->content_id) == FALSE) &part->content_id) == FALSE)
goto done; goto done;
} }
......
...@@ -80,9 +80,9 @@ typedef struct test { ...@@ -80,9 +80,9 @@ typedef struct test {
} Test; } Test;
static const MMSAttachmentInfo test_files_accept [] = { static const MMSAttachmentInfo test_files_accept [] = {
{ "smil", NULL, NULL }, { "smil", "application/smil;charset=us-ascii", NULL },
{ "0001.jpg", "image/jpeg", "image" }, { "0001.jpg", "image/jpeg;name=0001.jpg", "image" },
{ "test.txt", "text/plain;charset=utf-8", "text" } { "test.txt", "text/plain;charset=utf-8;name=wrong.name", "text" }
}; };
static const MMSAttachmentInfo test_files_accept_no_ext [] = { static const MMSAttachmentInfo test_files_accept_no_ext [] = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment