GithubHelp home page GithubHelp logo

miximka / mimeparser Goto Github PK

View Code? Open in Web Editor NEW
28.0 3.0 20.0 117 KB

Mime parsing in Swift | Relevant RFCs: RFC 822, RFC 2045, RFC 2046

License: MIT License

Swift 98.11% Ruby 1.11% C 0.78%
mime swift parser parsing rfc-822

mimeparser's Introduction

Build Status

About

MimeParser is a simple MIME (Multipurpose Internet Mail Extensions) parsing library written in Swift (to learn more about mimes refer to RFC 822, RFC 2045, RFC 2046)

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate MimeParser into your Xcode project using CocoaPods, specify it in your Podfile:

project '<Your Project Name>.xcodeproj'
platform :osx, '10.12'

target 'Test' do
  use_frameworks!
  pod 'MimeParser', '~> 0.1'
end

Then, run the following command:

$ pod install

Usage

Import MimeParser before using it:

import MimeParser

Create parser object:

let parser = MimeParser()

Let this be a simplest mime to be parsed:

let str = """
	Content-Type: text/plain
	
	Test
	"""

You are ready to parse the mime:

let mime = try parser.parse(str)

Returned mime object is a root of the mime tree and provides access to its header fields and content:

public enum MimeContent {
    case body(MimeBody)
    case mixed([Mime])
    case alternative([Mime])
}

public struct MimeHeader {
    public let contentTransferEncoding: ContentTransferEncoding?
    public let contentType: ContentType?
    public let contentDisposition: ContentDisposition?
    public let other: [RFC822HeaderField]
}

if let contentTypeString = mime.header.contentType?.raw {
	print("\(contentTypeString)")
	// "text/plain"
}

if case .body(let body) = mime.content {
	print("\(body.raw)")
	// "Test"
}

Decoded mime's content is simply to retrieve:

let content = try mime.decodedContentData()
// "Test"

License

MimeParser is available under the MIT license. See the LICENSE file for more info.

Contribution

MimeParser is still very simple and incomplete, so pull requests welcome!

mimeparser's People

Contributors

guidedways avatar jasonm23 avatar joachimm7 avatar miximka avatar nawar avatar slashdevslashgnoll avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mimeparser's Issues

Fails to decode utf8 text in some cases

In the odd event that an attachment has content-type like this:

Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit

This may be an odd combination, but it is definitely out there in the wild and should be handled even if it is "non-standard"

The library tries to decode it as ".ascii" here:

guard let decoded = raw.data(using: .ascii) else { throw Error.decodingFailed }

Even though the content-transfer-encoding is specified as 7bit, the data is actually utf-8 and standard ascii decoding will fail.
The charset should possibly override the transfer encoding, or possibly the decoder could fall-back to trying .utf8 if the .ascii decode fails. For example:

     guard let decoded = raw.data(using: .ascii) else {
            raw.data(using: .utf8) else { throw Error.decodingFailed }
     }

Header parser fails with empty headers

Parsing the attached NNTP message with MimeParser fails to include the "Subject" header, because it is preceded by a "Cc" header with no content:

Cc: 
Subject: Emacs implementations, list of, regular post [long, FAQ]

The problem is the regular expression used to parse the headers in RFC822HeaderFieldsPartitioner.fields():

        let regex = try! NSRegularExpression(pattern: "(.+?):\\s*(.+)", options: [])

It would probably be better to parse these 1 line at a time.

EmptyCc.txt

Encoded words support

First of all thank you for this library. I've used it to retrieve some headers, and I stumbled upon a header that contained UTF-8 data, is there a way to decode this header using this library ?

Format seems to be ?=encoding?charset?mytexthere?=

Thank you.

Headers are not case sensitive

Great parser! But header names are not case sensitive, so looking for "Content-Type" in HeaderParser.parse is a bit too strict.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.