Giter Site home page Giter Site logo

cartoonchess / labssearch-characterencoder Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 52 KB

Swift tool to find the character encoding of a webpage and format URLs the server will understand

License: MIT License

Swift 100.00%
charset charset-detector percent-encoding swift ios

labssearch-characterencoder's Introduction

LabsSearch CharacterEncoder

A set of tools in Swift to find the character encoding of a webpage and, later, format URLs in that encoding so that site's server will understand.

Project background

This class is broken out of LabsSearch (an early attempt at coding, so the code here, too, is messy) and can theoretically be used in other projects. It overcomes the headaches involved mainly with creating URLs that use GET on sites that do not use UTF-8 encoding.

Usage

Get webpage encoding

var characterEncoder: CharacterEncoder?
var encoding: String.Encoding? {
    characterEncoder?.encoding.value
}
let url = URL(string: "https://www.example.com/")!

let task = URLSession.shared.dataTask(with: url) { _, response, _ in
    let encodingName = response?.textEncodingName ?? "nil"
    self.characterEncoder = CharacterEncoder(encoding: encodingName)
}
task.resume()

Covert string to percent-encoded URL

let urlString = url.absoluteString
var encodedUrl: URL? = urlString.encodedUrl(characterEncoder: characterEncoder)

Change encoding of percent-encoded URL

let newEncoder = CharacterEncoder(encoding: "euc-kr")
CharacterEncoder.updateCharacterEncoding(newEncoder: newEncoder,
                                         oldEncoder: characterEncoder,
                                         url: encodedUrl!) { encoder, url in
    self.characterEncoder = encoder
    self.encodedUrl = url
}

Get query items

let components = URLComponents(url: encodedUrl!, resolvingAgainstBaseURL: true)
let encodedQueryItems = components?.queryItems(encoder: characterEncoder, keepPercentEncoding: true)

Files

Swift file Description
CharacterEncoder Takes an encoding name and URL and returns the properly formatted URL
CharacterEncoder+updateEncoding Extension for safely re-encoding an already encoded URL with a different encoding
CharacterSet+union Convenience functions for mixing and matching CharacterSets
CharacterSet+urlAllowedSets Various URL-safe CharacterSets
String+encodedUrl String extension to convert URL as String into encoded URL
String.Encoding+alias Converts human-readable encoding names to Swift equivalents
URLComponents+URLEncodedQueryItem Convenience functions to handle encoded query items regardless of iOS version or nil values

To do

Currently only the encoding as set in the response header is retrieved. XMLParser could be used to check meta tags as well.

Attributions

The encoding list is adapted from WHATWG.

The code itself is based on code which manually mapped each ASCII character and outputted a string for use with Korean encoding. I found the same code posted in multiple places without any attribution, so I admit I'm unsure who wrote the original. If you know, please speak up!

labssearch-characterencoder's People

Contributors

cartoonchess avatar

Watchers

 avatar

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.