http-grammar (empty) → 0.1.0.0
raw patch · 6 files changed
+465/−0 lines, 6 filesdep +attoparsecdep +basedep +bytestringsetup-changed
Dependencies added: attoparsec, base, bytestring
Files
- ChangeLog.md +5/−0
- LICENSE +202/−0
- README.md +19/−0
- Setup.hs +2/−0
- http-grammar.cabal +46/−0
- src/Network/HTTP/Grammar.hs +191/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for http-grammar++## 0.1.0.0 -- 2017-02-25++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,202 @@++ Apache License+ Version 2.0, January 2004+ http://www.apache.org/licenses/++ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION++ 1. Definitions.++ "License" shall mean the terms and conditions for use, reproduction,+ and distribution as defined by Sections 1 through 9 of this document.++ "Licensor" shall mean the copyright owner or entity authorized by+ the copyright owner that is granting the License.++ "Legal Entity" shall mean the union of the acting entity and all+ other entities that control, are controlled by, or are under common+ control with that entity. For the purposes of this definition,+ "control" means (i) the power, direct or indirect, to cause the+ direction or management of such entity, whether by contract or+ otherwise, or (ii) ownership of fifty percent (50%) or more of the+ outstanding shares, or (iii) beneficial ownership of such entity.++ "You" (or "Your") shall mean an individual or Legal Entity+ exercising permissions granted by this License.++ "Source" form shall mean the preferred form for making modifications,+ including but not limited to software source code, documentation+ source, and configuration files.++ "Object" form shall mean any form resulting from mechanical+ transformation or translation of a Source form, including but+ not limited to compiled object code, generated documentation,+ and conversions to other media types.++ "Work" shall mean the work of authorship, whether in Source or+ Object form, made available under the License, as indicated by a+ copyright notice that is included in or attached to the work+ (an example is provided in the Appendix below).++ "Derivative Works" shall mean any work, whether in Source or Object+ form, that is based on (or derived from) the Work and for which the+ editorial revisions, annotations, elaborations, or other modifications+ represent, as a whole, an original work of authorship. For the purposes+ of this License, Derivative Works shall not include works that remain+ separable from, or merely link (or bind by name) to the interfaces of,+ the Work and Derivative Works thereof.++ "Contribution" shall mean any work of authorship, including+ the original version of the Work and any modifications or additions+ to that Work or Derivative Works thereof, that is intentionally+ submitted to Licensor for inclusion in the Work by the copyright owner+ or by an individual or Legal Entity authorized to submit on behalf of+ the copyright owner. For the purposes of this definition, "submitted"+ means any form of electronic, verbal, or written communication sent+ to the Licensor or its representatives, including but not limited to+ communication on electronic mailing lists, source code control systems,+ and issue tracking systems that are managed by, or on behalf of, the+ Licensor for the purpose of discussing and improving the Work, but+ excluding communication that is conspicuously marked or otherwise+ designated in writing by the copyright owner as "Not a Contribution."++ "Contributor" shall mean Licensor and any individual or Legal Entity+ on behalf of whom a Contribution has been received by Licensor and+ subsequently incorporated within the Work.++ 2. Grant of Copyright License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ copyright license to reproduce, prepare Derivative Works of,+ publicly display, publicly perform, sublicense, and distribute the+ Work and such Derivative Works in Source or Object form.++ 3. Grant of Patent License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ (except as stated in this section) patent license to make, have made,+ use, offer to sell, sell, import, and otherwise transfer the Work,+ where such license applies only to those patent claims licensable+ by such Contributor that are necessarily infringed by their+ Contribution(s) alone or by combination of their Contribution(s)+ with the Work to which such Contribution(s) was submitted. If You+ institute patent litigation against any entity (including a+ cross-claim or counterclaim in a lawsuit) alleging that the Work+ or a Contribution incorporated within the Work constitutes direct+ or contributory patent infringement, then any patent licenses+ granted to You under this License for that Work shall terminate+ as of the date such litigation is filed.++ 4. Redistribution. You may reproduce and distribute copies of the+ Work or Derivative Works thereof in any medium, with or without+ modifications, and in Source or Object form, provided that You+ meet the following conditions:++ (a) You must give any other recipients of the Work or+ Derivative Works a copy of this License; and++ (b) You must cause any modified files to carry prominent notices+ stating that You changed the files; and++ (c) You must retain, in the Source form of any Derivative Works+ that You distribute, all copyright, patent, trademark, and+ attribution notices from the Source form of the Work,+ excluding those notices that do not pertain to any part of+ the Derivative Works; and++ (d) If the Work includes a "NOTICE" text file as part of its+ distribution, then any Derivative Works that You distribute must+ include a readable copy of the attribution notices contained+ within such NOTICE file, excluding those notices that do not+ pertain to any part of the Derivative Works, in at least one+ of the following places: within a NOTICE text file distributed+ as part of the Derivative Works; within the Source form or+ documentation, if provided along with the Derivative Works; or,+ within a display generated by the Derivative Works, if and+ wherever such third-party notices normally appear. The contents+ of the NOTICE file are for informational purposes only and+ do not modify the License. You may add Your own attribution+ notices within Derivative Works that You distribute, alongside+ or as an addendum to the NOTICE text from the Work, provided+ that such additional attribution notices cannot be construed+ as modifying the License.++ You may add Your own copyright statement to Your modifications and+ may provide additional or different license terms and conditions+ for use, reproduction, or distribution of Your modifications, or+ for any such Derivative Works as a whole, provided Your use,+ reproduction, and distribution of the Work otherwise complies with+ the conditions stated in this License.++ 5. Submission of Contributions. Unless You explicitly state otherwise,+ any Contribution intentionally submitted for inclusion in the Work+ by You to the Licensor shall be under the terms and conditions of+ this License, without any additional terms or conditions.+ Notwithstanding the above, nothing herein shall supersede or modify+ the terms of any separate license agreement you may have executed+ with Licensor regarding such Contributions.++ 6. Trademarks. This License does not grant permission to use the trade+ names, trademarks, service marks, or product names of the Licensor,+ except as required for reasonable and customary use in describing the+ origin of the Work and reproducing the content of the NOTICE file.++ 7. Disclaimer of Warranty. Unless required by applicable law or+ agreed to in writing, Licensor provides the Work (and each+ Contributor provides its Contributions) on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or+ implied, including, without limitation, any warranties or conditions+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A+ PARTICULAR PURPOSE. You are solely responsible for determining the+ appropriateness of using or redistributing the Work and assume any+ risks associated with Your exercise of permissions under this License.++ 8. Limitation of Liability. In no event and under no legal theory,+ whether in tort (including negligence), contract, or otherwise,+ unless required by applicable law (such as deliberate and grossly+ negligent acts) or agreed to in writing, shall any Contributor be+ liable to You for damages, including any direct, indirect, special,+ incidental, or consequential damages of any character arising as a+ result of this License or out of the use or inability to use the+ Work (including but not limited to damages for loss of goodwill,+ work stoppage, computer failure or malfunction, or any and all+ other commercial damages or losses), even if such Contributor+ has been advised of the possibility of such damages.++ 9. Accepting Warranty or Additional Liability. While redistributing+ the Work or Derivative Works thereof, You may choose to offer,+ and charge a fee for, acceptance of support, warranty, indemnity,+ or other liability obligations and/or rights consistent with this+ License. However, in accepting such obligations, You may act only+ on Your own behalf and on Your sole responsibility, not on behalf+ of any other Contributor, and only if You agree to indemnify,+ defend, and hold each Contributor harmless for any liability+ incurred by, or claims asserted against, such Contributor by reason+ of your accepting any such warranty or additional liability.++ END OF TERMS AND CONDITIONS++ APPENDIX: How to apply the Apache License to your work.++ To apply the Apache License to your work, attach the following+ boilerplate notice, with the fields enclosed by brackets "[]"+ replaced with your own identifying information. (Don't include+ the brackets!) The text should be enclosed in the appropriate+ comment syntax for the file format. We also recommend that a+ file or class name and description of purpose be included on the+ same "printed page" as the copyright notice for easier+ identification within third-party archives.++ Copyright [yyyy] [name of copyright owner]++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.
+ README.md view
@@ -0,0 +1,19 @@+# http-grammar++`http-grammar` is a Haskell package that provides Attoparsec-based+productions that are faithful to the HTTP grammar specified in RFC-2616.++The guiding philosophy of this package is to prioritize precision and+faithfulness to RFC-2616 over all other considerations.++Contributions are welcome and encouraged! The development plan for this+package is to add productions from time to time as they are needed in+my other work, and to solicit the contributions of productions from you+as they are needed in your other work.++I'm also willing to take requests. If there is a production you need but for+whatever reason can't or don't want to implement, just submit a github issue.++Thanks!++
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ http-grammar.cabal view
@@ -0,0 +1,46 @@+-- Initial http-grammar.cabal generated by cabal init. For further+-- documentation, see http://haskell.org/cabal/users-guide/++name: http-grammar+version: 0.1.0.0+synopsis: Attoparsec-based parsers for the RFC-2616 HTTP grammar rules.+description: The purpose of this package is to provide a faithful+ implementation of the RFC-2616 HTTP grammar rules without+ concession to popular deviations that "happen in the+ wild".++ The implementation is far from complete. My development+ plan for this package is to add various productions from+ time to time as I need them in other work I'm doing.++ Contributions are most welcome and highly encouraged.+homepage: https://github.com/owensmurray/http-grammar+license: Apache-2.0+license-file: LICENSE+author: Rick Owens+maintainer: rowens@owensmurray.com+copyright: 2017 Owens Murray LLC+category: Web+build-type: Simple+extra-source-files:+ ChangeLog.md+ README.md+cabal-version: >=1.10++library+ exposed-modules:+ Network.HTTP.Grammar+ -- other-modules:+ other-extensions:+ OverloadedStrings+ build-depends:+ attoparsec >= 0.12.1.6 && < 0.14,+ bytestring >= 0.10.4.0 && < 0.11,+ base >= 4.7.0.2 && < 4.10+ hs-source-dirs: src+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/owensmurray/http-grammar.git+
+ src/Network/HTTP/Grammar.hs view
@@ -0,0 +1,191 @@+{-# LANGUAGE OverloadedStrings #-}++{- |+ This module provides Attoparsec-based parsers for the HTTP grammar+ rules as defined by RFC-2616.+-}+module Network.HTTP.Grammar (+ -- * Parsable data types.+ UserAgent(..),+ Product(..),++ -- * Productions+ userAgent,+ product,+ comment,+ ctext,+ quotedPair,+ text,+ char,+ ctl,+ token,+ separators,+ lws,+ crlf,+) where++import Prelude hiding (product)++import Control.Applicative (many, (<|>), (<$>), (<*>))+import Control.Monad (void)+import Data.Attoparsec.ByteString (Parser, word8, takeWhile1, satisfy,+ inClass, option, string, many1)+import Data.ByteString (ByteString)+import Data.Monoid (mconcat)+import Data.Word (Word8)+import qualified Data.ByteString as BS+++{- |+ The User-Agent header field is defined as a list of tokens, each+ of which is either a product or a comment. Values of this data type+ represents one such token.+-}+data UserAgent+ = UAProduct Product+ | UAComment ByteString+++{- |+ A representation of an HTTP User-Agent product.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.8+-}+data Product = Product {+ productName :: ByteString,+ productVersion :: Maybe ByteString+ }+++{- |+ Parser for the User-Agent header, defined:+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43+-}+userAgent :: Parser [UserAgent]+userAgent = many1 (+ (UAProduct <$> product)+ <|> (UAComment <$> comment)+ )+++{- |+ product - http product token.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.8+-}+product :: Parser Product+product =+ Product+ <$> token+ <*> option Nothing versionParser + where+ versionParser = do+ void $ string "/"+ Just <$> token+++{- |+ comment - basic http grammar rule.+ The result is the parsed comment content, rather than the raw source.++ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+comment :: Parser ByteString+comment = do+ void $ word8 40 -- "("+ segments <- many (+ takeWhile1 ctext+ <|> (BS.singleton <$> quotedPair)+ <|> comment+ )+ void $ word8 41 -- ")"+ return (mconcat segments)+++{- |+ ctext - basic http grammar rule.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+ctext :: Word8 -> Bool+ctext x = text x && x /= 40 && x /= 41 -- "(", ")"+++{- |+ quotedPair - basic http grammar rule.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+quotedPair :: Parser Word8+quotedPair = do+ void $ word8 92 -- <\>+ satisfy char+++{- |+ TEXT - basic http grammar rule.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+text :: Word8 -> Bool+text = not . ctl+++{- |+ CHAR - basic http grammar rule.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+char :: Word8 -> Bool+char x = x >= 0 && x <= 127+++{- |+ CTL - basic http grammar rule.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+ctl :: Word8 -> Bool+ctl x = (x >= 0 && x <= 31) || x == 127+++{- |+ token - basic http grammar rule.++ The grammar specifies that adjacent LWS should be consumed without affecting+ the meaning of the token. This parser returns the token stripped of any+ adjacent lws.++ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+token :: Parser ByteString+token = do+ option () lws+ t <- takeWhile1 (\x -> char x && not (separators x) && not (ctl x))+ option () lws+ return t++ +{- |+ separators - basic http grammar rule.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+separators :: Word8 -> Bool+separators = inClass "()<>@,;:\\\"/[]?={} \t"+++{- |+ LWS - basic http grammar rule. (L)inear (W)hite(S)pace.++ Consumes all linear whitespace.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+lws :: Parser ()+lws = do+ option () crlf+ void $ takeWhile1 (\x -> x == 32 || x == 9)+++{- |+ crlf - basic http grammar rule.++ Consumes one crlf.+ https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2+-}+crlf :: Parser ()+crlf = void $ string "\r\n"++