├── .github ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── feature_request.md │ └── question.md ├── PULL_REQUEST_TEMPLATE.md └── stale.yml ├── .gitignore ├── .travis.yml ├── CERTIFICATE ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── rustfmt.toml ├── src ├── error.rs ├── frames.rs ├── frames │ ├── data_frame.rs │ ├── frame_flag.rs │ ├── frame_header.rs │ ├── frame_header_inflate_flags.rs │ ├── frame_kind.rs │ ├── headers_frame.rs │ ├── headers_kind.rs │ └── settings_frame_kind.rs ├── lib.rs ├── name_value_flags.rs ├── session.rs ├── stream.rs └── stream │ ├── stream_exclusivity.rs │ ├── stream_priority.rs │ └── stream_state.rs └── tests └── test.rs /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of 9 | experience, 10 | education, socio-economic status, nationality, personal appearance, race, 11 | religion, or sexual identity and orientation. 12 | 13 | ## Our Standards 14 | 15 | Examples of behavior that contributes to creating a positive environment 16 | include: 17 | 18 | - Using welcoming and inclusive language 19 | - Being respectful of differing viewpoints and experiences 20 | - Gracefully accepting constructive criticism 21 | - Focusing on what is best for the community 22 | - Showing empathy towards other community members 23 | 24 | Examples of unacceptable behavior by participants include: 25 | 26 | - The use of sexualized language or imagery and unwelcome sexual attention or 27 | advances 28 | - Trolling, insulting/derogatory comments, and personal or political attacks 29 | - Public or private harassment 30 | - Publishing others' private information, such as a physical or electronic 31 | address, without explicit permission 32 | - Other conduct which could reasonably be considered inappropriate in a 33 | professional setting 34 | 35 | 36 | ## Our Responsibilities 37 | 38 | Project maintainers are responsible for clarifying the standards of acceptable 39 | behavior and are expected to take appropriate and fair corrective action in 40 | response to any instances of unacceptable behavior. 41 | 42 | Project maintainers have the right and responsibility to remove, edit, or 43 | reject comments, commits, code, wiki edits, issues, and other contributions 44 | that are not aligned to this Code of Conduct, or to ban temporarily or 45 | permanently any contributor for other behaviors that they deem inappropriate, 46 | threatening, offensive, or harmful. 47 | 48 | ## Scope 49 | 50 | This Code of Conduct applies both within project spaces and in public spaces 51 | when an individual is representing the project or its community. Examples of 52 | representing a project or community include using an official project e-mail 53 | address, posting via an official social media account, or acting as an appointed 54 | representative at an online or offline event. Representation of a project may be 55 | further defined and clarified by project maintainers. 56 | 57 | ## Enforcement 58 | 59 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 60 | reported by contacting the project team at yoshuawuyts@gmail.com, or through 61 | IRC. All complaints will be reviewed and investigated and will result in a 62 | response that is deemed necessary and appropriate to the circumstances. The 63 | project team is obligated to maintain confidentiality with regard to the 64 | reporter of an incident. 65 | Further details of specific enforcement policies may be posted separately. 66 | 67 | Project maintainers who do not follow or enforce the Code of Conduct in good 68 | faith may face temporary or permanent repercussions as determined by other 69 | members of the project's leadership. 70 | 71 | ## Attribution 72 | 73 | This Code of Conduct is adapted from the Contributor Covenant, version 1.4, 74 | available at 75 | https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 76 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | Contributions include code, documentation, answering user questions, running the 3 | project's infrastructure, and advocating for all types of users. 4 | 5 | The project welcomes all contributions from anyone willing to work in good faith 6 | with other contributors and the community. No contribution is too small and all 7 | contributions are valued. 8 | 9 | This guide explains the process for contributing to the project's GitHub 10 | Repository. 11 | 12 | - [Code of Conduct](#code-of-conduct) 13 | - [Bad Actors](#bad-actors) 14 | - [Developer Certificate of Origin](#developer-certificate-of-origin) 15 | 16 | ## Code of Conduct 17 | The project has a [Code of Conduct](./CODE_OF_CONDUCT.md) that *all* 18 | contributors are expected to follow. This code describes the *minimum* behavior 19 | expectations for all contributors. 20 | 21 | As a contributor, how you choose to act and interact towards your 22 | fellow contributors, as well as to the community, will reflect back not only 23 | on yourself but on the project as a whole. The Code of Conduct is designed and 24 | intended, above all else, to help establish a culture within the project that 25 | allows anyone and everyone who wants to contribute to feel safe doing so. 26 | 27 | Should any individual act in any way that is considered in violation of the 28 | [Code of Conduct](./CODE_OF_CONDUCT.md), corrective actions will be taken. It is 29 | possible, however, for any individual to *act* in such a manner that is not in 30 | violation of the strict letter of the Code of Conduct guidelines while still 31 | going completely against the spirit of what that Code is intended to accomplish. 32 | 33 | Open, diverse, and inclusive communities live and die on the basis of trust. 34 | Contributors can disagree with one another so long as they trust that those 35 | disagreements are in good faith and everyone is working towards a common 36 | goal. 37 | 38 | ## Bad Actors 39 | All contributors to tacitly agree to abide by both the letter and 40 | spirit of the [Code of Conduct](./CODE_OF_CONDUCT.md). Failure, or 41 | unwillingness, to do so will result in contributions being respectfully 42 | declined. 43 | 44 | A *bad actor* is someone who repeatedly violates the *spirit* of the Code of 45 | Conduct through consistent failure to self-regulate the way in which they 46 | interact with other contributors in the project. In doing so, bad actors 47 | alienate other contributors, discourage collaboration, and generally reflect 48 | poorly on the project as a whole. 49 | 50 | Being a bad actor may be intentional or unintentional. Typically, unintentional 51 | bad behavior can be easily corrected by being quick to apologize and correct 52 | course *even if you are not entirely convinced you need to*. Giving other 53 | contributors the benefit of the doubt and having a sincere willingness to admit 54 | that you *might* be wrong is critical for any successful open collaboration. 55 | 56 | Don't be a bad actor. 57 | 58 | ## Developer Certificate of Origin 59 | All contributors must read and agree to the [Developer Certificate of 60 | Origin (DCO)](../CERTIFICATE). 61 | 62 | The DCO allows us to accept contributions from people to the project, similarly 63 | to how a license allows us to distribute our code. 64 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Summary 2 | Explain what is going on. 3 | 4 | ## Your Environment 5 | | Software | Version(s) | 6 | | ---------------- | ---------- | 7 | | nghttp2 | 8 | | Rustc | 9 | | Operating System | 10 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug Report 3 | about: Did something not work as expected? 4 | --- 5 | 6 | # Bug Report 7 | ## Your Environment 8 | | Software | Version(s) | 9 | | ---------------- | ---------- | 10 | | nghttp2 | 11 | | Rustc | 12 | | Operating System | 13 | 14 | ## Expected Behavior 15 | Tell us what should have happened. 16 | 17 | ## Current Behavior 18 | Tell us what happens instead of the expected behavior. If you are seeing an 19 | error, please include the full error message and stack trace. 20 | 21 | ## Code Sample 22 | Please provide a code repository, gist, code snippet or sample files to 23 | reproduce the issue. 24 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature Request 3 | about: Want us to add something to nghttp2? 4 | --- 5 | 6 | # Feature Request 7 | ## Summary 8 | One paragraph explanation of the feature. 9 | 10 | ## Motivation 11 | Why are we doing this? What use cases does it support? What is the expected 12 | outcome? 13 | 14 | ## Guide-level explanation 15 | Explain the proposal as if it was already included in the project and you 16 | were teaching it to another programmer. That generally means: 17 | 18 | - Introducing new named concepts. 19 | - Explaining the feature largely in terms of examples. 20 | - If applicable, provide sample error messages, deprecation warnings, or 21 | migration guidance. 22 | 23 | ## Reference-level explanation 24 | This is the technical portion of the feature request. Explain the design in 25 | sufficient detail that: 26 | 27 | - Its interaction with other features is clear. 28 | - It is reasonably clear how the feature would be implemented. 29 | - Corner cases are dissected by example. 30 | 31 | ## Drawbacks 32 | Why should we _not_ do this? 33 | 34 | ## Rationale and alternatives 35 | - Why is this design the best in the space of possible designs? 36 | - What other designs have been considered and what is the rationale for not 37 | choosing them? 38 | - What is the impact of not doing this? 39 | 40 | ## Unresolved Questions 41 | What related issues do you consider out of scope for this feature that could be 42 | addressed in the future independently of the solution that comes out of this 43 | feature? 44 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/question.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Question 3 | about: Have any questions regarding how nghttp2 works? 4 | --- 5 | 6 | # Question 7 | ## Your Environment 8 | | Software | Version(s) | 9 | | ---------------- | ---------- | 10 | | nghttp2 | 11 | | Rustc | 12 | | Operating System | 13 | 14 | ## Question 15 | Provide your question here. 16 | 17 | ## Context 18 | How has this issue affected you? What are you trying to accomplish? 19 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | **Choose one:** is this a 🐛 bug fix, a 🙋 feature, or a 🔦 documentation change? 8 | 9 | 10 | 11 | ## Checklist 12 | 13 | - [ ] tests pass 14 | - [ ] tests and/or benchmarks are included 15 | - [ ] documentation is changed or added 16 | 17 | ## Context 18 | 19 | 20 | ## Semver Changes 21 | 22 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Configuration for probot-stale - https://github.com/probot/stale 2 | 3 | daysUntilStale: 90 4 | daysUntilClose: 7 5 | exemptLabels: 6 | - pinned 7 | - security 8 | exemptProjects: false 9 | exemptMilestones: false 10 | staleLabel: wontfix 11 | markComment: > 12 | This issue has been automatically marked as stale because it has not had 13 | recent activity. It will be closed if no further activity occurs. Thank you 14 | for your contributions. 15 | unmarkComment: false 16 | closeComment: false 17 | limitPerRun: 30 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | coverage/ 2 | target/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | Cargo.lock 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - stable 4 | 5 | before_script: | 6 | rustup component add rustfmt-preview && 7 | rustup component add clippy-preview 8 | script: | 9 | cargo fmt -- --check && 10 | cargo clippy -- -D clippy && 11 | cargo build --verbose && 12 | cargo test --verbose 13 | cache: cargo 14 | -------------------------------------------------------------------------------- /CERTIFICATE: -------------------------------------------------------------------------------- 1 | Developer Certificate of Origin 2 | Version 1.1 3 | 4 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 5 | 1 Letterman Drive 6 | Suite D4700 7 | San Francisco, CA, 94129 8 | 9 | Everyone is permitted to copy and distribute verbatim copies of this 10 | license document, but changing it is not allowed. 11 | 12 | 13 | Developer's Certificate of Origin 1.1 14 | 15 | By making a contribution to this project, I certify that: 16 | 17 | (a) The contribution was created in whole or in part by me and I 18 | have the right to submit it under the open source license 19 | indicated in the file; or 20 | 21 | (b) The contribution is based upon previous work that, to the best 22 | of my knowledge, is covered under an appropriate open source 23 | license and I have the right under that license to submit that 24 | work with modifications, whether created in whole or in part 25 | by me, under the same open source license (unless I am 26 | permitted to submit under a different license), as indicated 27 | in the file; or 28 | 29 | (c) The contribution was provided directly to me by some other 30 | person who certified (a), (b) or (c) and I have not modified 31 | it. 32 | 33 | (d) I understand and agree that this project and the contribution 34 | are public and that a record of the contribution (including all 35 | personal information I submit with it, including my sign-off) is 36 | maintained indefinitely and may be redistributed consistent with 37 | this project or the open source license(s) involved. 38 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "nghttp2" 3 | version = "0.0.0" 4 | license = "MIT OR Apache-2.0" 5 | repository = "https://github.com/rust-net-web/nghttp2" 6 | documentation = "https://docs.rs/nghttp2" 7 | description = "High-level Nghttp2 API." 8 | keywords = ["http2", "h2", "parser", "duplex", "connection"] 9 | categories = ["asynchronous", "network-programming", "web-programming", "web-programming::http-client", "web-programming::http-server"] 10 | authors = ["Yoshua Wuyts "] 11 | readme = "README.md" 12 | edition = "2018" 13 | 14 | [dependencies] 15 | failure = "0.1.3" 16 | libnghttp2-sys = "0.1.1" 17 | openssl = "0.10.16" 18 | bitflags = "1.0.4" 19 | 20 | [dev-dependencies] 21 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright 2018 Yoshua Wuyts 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | http://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. 191 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Yoshua Wuyts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nghttp2 2 | [![crates.io version][1]][2] [![build status][3]][4] 3 | [![downloads][5]][6] [![docs.rs docs][7]][8] 4 | 5 | High-level [`Nghttp2`](https://github.com/nghttp2/nghttp2) API. 6 | 7 | - [Documentation][8] 8 | - [Crates.io][2] 9 | - [Releases][releases] 10 | 11 | ## Examples 12 | __Basic usage__ 13 | ```rust 14 | // tbi 15 | ``` 16 | 17 | ## Installation 18 | ```sh 19 | $ cargo add nghttp2 20 | ``` 21 | 22 | ## Safety 23 | This crate provides safe bindings to the `libnghttp2` C library. 24 | 25 | ## Contributing 26 | Want to join us? Check out our ["Contributing" guide][contributing] and take a 27 | look at some of these issues: 28 | 29 | - [Issues labeled "good first issue"][good-first-issue] 30 | - [Issues labeled "help wanted"][help-wanted] 31 | 32 | ## References 33 | - [nghttp2/nghttp2](https://github.com/nghttp2/nghttp2) 34 | - [alexcrichton/nghttp2-rs](https://github.com/alexcrichton/nghttp2-rs) 35 | - [libnghttp2 server tutorial](https://www.nghttp2.org/documentation/tutorial-server.html) 36 | 37 | ## License 38 | [MIT](./LICENSE-MIT) OR [Apache-2.0](./LICENSE-APACHE) 39 | 40 | [1]: https://img.shields.io/crates/v/nghttp2.svg?style=flat-square 41 | [2]: https://crates.io/crates/nghttp2 42 | [3]: https://img.shields.io/travis/http-rs/nghttp2/main.svg?style=flat-square 43 | [4]: https://travis-ci.org/http-rs/nghttp2 44 | [5]: https://img.shields.io/crates/d/nghttp2.svg?style=flat-square 45 | [6]: https://crates.io/crates/nghttp2 46 | [7]: https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square 47 | [8]: https://docs.rs/nghttp2 48 | 49 | [releases]: https://github.com/http-rs/nghttp2/releases 50 | [contributing]: https://github.com/http-rs/nghttp2/blob/main.github/CONTRIBUTING.md 51 | [good-first-issue]: https://github.com/http-rs/nghttp2/labels/good%20first%20issue 52 | [help-wanted]: https://github.com/http-rs/nghttp2/labels/help%20wanted 53 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | max_width = 80 2 | tab_spaces = 2 3 | -------------------------------------------------------------------------------- /src/error.rs: -------------------------------------------------------------------------------- 1 | use failure::{self, Backtrace, Context, Fail}; 2 | use std::fmt::{self, Display}; 3 | use std::result; 4 | 5 | use libnghttp2_sys::nghttp2_error; 6 | 7 | /// A specialized [`Result`] type for this crate's operations. 8 | /// 9 | /// This is generally used to avoid writing out [Error] directly and 10 | /// is otherwise a direct mapping to [`Result`]. 11 | /// 12 | /// [`Result`]: https://doc.rust-lang.org/nightly/std/result/enum.Result.html 13 | /// [`Error`]: std.struct.Error.html 14 | pub type Result = result::Result; 15 | 16 | /// A list enumerating the categories of errors. 17 | /// 18 | /// This list is intended to grow over time and it is not recommended to 19 | /// exhaustively match against it. 20 | /// 21 | /// It is used with the [`Error`] struct. 22 | /// 23 | /// [`Error`]: std.struct.Error.html 24 | #[derive(Debug, Fail)] 25 | pub enum ErrorKind { 26 | /// Invalid argument passed. 27 | #[fail(display = "Invalid argument passed.")] 28 | InvalidArgument, 29 | 30 | /// Out of buffer space. 31 | #[fail(display = "Out of buffer space.")] 32 | BufferError, 33 | 34 | /// Out of buffer space. 35 | #[fail(display = "The specified protocol version is not supported.")] 36 | UnsupportedVersion, 37 | 38 | /// Indicate that the operation would block. 39 | #[fail(display = "Indicate that the operation would block.")] 40 | WouldBlock, 41 | 42 | /// General protocol error 43 | #[fail(display = "General protocol error")] 44 | Proto, 45 | 46 | /// The frame is invalid. 47 | #[fail(display = "The frame is invalid.")] 48 | InvalidFrame, 49 | 50 | /// The peer performed a shutdown on the connection. 51 | #[fail(display = "The peer performed a shutdown on the connection.")] 52 | Eof, 53 | 54 | /// Used as a return value from :func:nghttp2DataSourceReadCallback to indicate that data transfer is postponed. See :func:nghttp2DataSourceReadCallback for details. 55 | #[fail( 56 | display = "Used as a return value from :func:nghttp2DataSourceReadCallback to indicate that data transfer is postponed. See :func:nghttp2DataSourceReadCallback for details." 57 | )] 58 | Deferred, 59 | 60 | /// Stream ID has reached the maximum value. Therefore no stream ID is available. 61 | #[fail( 62 | display = "Stream ID has reached the maximum value. Therefore no stream ID is available." 63 | )] 64 | StreamIdNotAvailable, 65 | 66 | /// The stream is already closed; or the stream ID is invalid. 67 | #[fail( 68 | display = "The stream is already closed; or the stream ID is invalid." 69 | )] 70 | StreamClosed, 71 | 72 | /// RST_STREAM has been added to the outbound queue. The stream is in closing state. 73 | #[fail( 74 | display = "RST_STREAM has been added to the outbound queue. The stream is in closing state." 75 | )] 76 | StreamClosing, 77 | 78 | /// The transmission is not allowed for this stream (e.g., a frame with END_STREAM flag set has already sent). 79 | #[fail( 80 | display = "The transmission is not allowed for this stream (e.g., a frame with END_STREAM flag set has already sent)." 81 | )] 82 | StreamShutWr, 83 | 84 | /// The stream ID is invalid. 85 | #[fail(display = "The stream ID is invalid.")] 86 | InvalidStreamId, 87 | 88 | /// The state of the stream is not valid (e.g., DATA cannot be sent to the stream if response HEADERS has not been sent). 89 | #[fail( 90 | display = "The state of the stream is not valid (e.g., DATA cannot be sent to the stream if response HEADERS has not been sent)." 91 | )] 92 | InvalidStreamState, 93 | 94 | /// Another DATA frame has already been deferred. 95 | #[fail(display = "Another DATA frame has already been deferred.")] 96 | DeferredDataExist, 97 | 98 | /// Starting new stream is not allowed (e.g., GOAWAY has been sent and/or received). 99 | #[fail( 100 | display = "Starting new stream is not allowed (e.g., GOAWAY has been sent and/or received)." 101 | )] 102 | StartStreamNotAllowed, 103 | 104 | /// GOAWAY has already been sent. 105 | #[fail(display = "GOAWAY has already been sent.")] 106 | GoawayAlreadySent, 107 | 108 | /// The received frame contains the invalid header block (e.g., There are duplicate header names; or the header names are not encoded in US-ASCII character set and not lower cased; or the header name is zero-length string; or the header value contains multiple in-sequence NUL bytes). 109 | #[fail( 110 | display = "The received frame contains the invalid header block (e.g., There are duplicate header names; or the header names are not encoded in US-ASCII character set and not lower cased; or the header name is zero-length string; or the header value contains multiple in-sequence NUL bytes)." 111 | )] 112 | InvalidHeaderBlock, 113 | 114 | /// Indicates that the context is not suitable to perform the requested operation. 115 | #[fail( 116 | display = "Indicates that the context is not suitable to perform the requested operation." 117 | )] 118 | InvalidState, 119 | 120 | /// The user callback function failed due to the temporal error. 121 | #[fail( 122 | display = "The user callback function failed due to the temporal error." 123 | )] 124 | TemporalCallbackFailure, 125 | 126 | /// The length of the frame is invalid, either too large or too small. 127 | #[fail( 128 | display = "The length of the frame is invalid, either too large or too small." 129 | )] 130 | FrameSizeError, 131 | 132 | /// Header block inflate/deflate error. 133 | #[fail(display = "Header block inflate/deflate error.")] 134 | HeaderComp, 135 | 136 | /// Flow control error 137 | #[fail(display = "Flow control error")] 138 | FlowControl, 139 | 140 | /// Insufficient buffer size given to function. 141 | #[fail(display = "Insufficient buffer size given to function.")] 142 | InsuffBufsize, 143 | 144 | /// Callback was paused by the application 145 | #[fail(display = "Callback was paused by the application")] 146 | Pause, 147 | 148 | /// There are too many in-flight SETTING frame and no more transmission of SETTINGS is allowed. 149 | #[fail( 150 | display = "There are too many in-flight SETTING frame and no more transmission of SETTINGS is allowed." 151 | )] 152 | TooManyInflightSettings, 153 | 154 | /// The server push is disabled. 155 | #[fail(display = "The server push is disabled.")] 156 | PushDisabled, 157 | 158 | /// DATA or HEADERS frame for a given stream has been already submitted and has not been fully processed yet. Application should wait for the transmission of the previously submitted frame before submitting another. 159 | #[fail( 160 | display = "DATA or HEADERS frame for a given stream has been already submitted and has not been fully processed yet. Application should wait for the transmission of the previously submitted frame before submitting another." 161 | )] 162 | DataExist, 163 | 164 | /// The current session is closing due to a connection error or nghttp2SessionTerminateSession() is called. 165 | #[fail( 166 | display = "The current session is closing due to a connection error or nghttp2SessionTerminateSession() is called." 167 | )] 168 | SessionClosing, 169 | 170 | /// Invalid HTTP header field was received and stream is going to be closed. 171 | #[fail( 172 | display = "Invalid HTTP header field was received and stream is going to be closed." 173 | )] 174 | HttpHeader, 175 | 176 | /// Violation in HTTP messaging rule. 177 | #[fail(display = "Violation in HTTP messaging rule.")] 178 | HttpMessaging, 179 | 180 | /// Stream was refused. 181 | #[fail(display = "Stream was refused.")] 182 | RefusedStream, 183 | 184 | /// Unexpected internal error, but recovered. 185 | #[fail(display = "Unexpected internal error, but recovered.")] 186 | Internal, 187 | 188 | /// Indicates that a processing was canceled. 189 | #[fail(display = "Indicates that a processing was canceled.")] 190 | Cancel, 191 | 192 | /// When a local endpoint expects to receive SETTINGS frame, it receives an other type of frame. 193 | #[fail( 194 | display = "When a local endpoint expects to receive SETTINGS frame, it receives an other type of frame." 195 | )] 196 | SettingsExpected, 197 | 198 | /// The errors < :enum:FATAL mean that the library is under unexpected condition and processing was terminated (e.g., out of memory). If application receives this error code, it must stop using that :type:nghttp2Session object and only allowed operation for that object is deallocate it using nghttp2SessionDel(). 199 | #[fail( 200 | display = "The errors < :enum:FATAL mean that the library is under unexpected condition and processing was terminated (e.g., out of memory). If application receives this error code, it must stop using that :type:nghttp2Session object and only allowed operation for that object is deallocate it using nghttp2SessionDel()." 201 | )] 202 | Fatal, 203 | 204 | /// Out of memory. This is a fatal error. 205 | #[fail(display = "Out of memory. This is a fatal error.")] 206 | Nomem, 207 | 208 | /// The user callback function failed. This is a fatal error. 209 | #[fail( 210 | display = "The user callback function failed. This is a fatal error." 211 | )] 212 | CallbackFailure, 213 | 214 | /// Invalid client magic (see :macro:NGHTTP2_CLIENT_MAGIC) was received and further processing is not possible. 215 | #[fail( 216 | display = "Invalid client magic (see :macro:NGHTTP2_CLIENT_MAGIC) was received and further processing is not possible." 217 | )] 218 | BadClientMagic, 219 | 220 | /// Possible flooding by peer was detected in this HTTP/2 session. Flooding is measured by how many PING and SETTINGS frames with ACK flag set are queued for transmission. These frames are response for the peer initiated frames, and peer can cause memory exhaustion on server side. 221 | #[fail( 222 | display = "Possible flooding by peer was detected in this HTTP/2 session. Flooding is measured by how many PING and SETTINGS frames with ACK flag set are queued for transmission. These frames are response for the peer initiated frames, and peer can cause memory exhaustion on server side." 223 | )] 224 | Flooded, 225 | } 226 | 227 | /// A specialized [`Error`] type for this crate's operations. 228 | /// 229 | /// [`Error`]: https://doc.rust-lang.org/nightly/std/error/trait.Error.html 230 | #[derive(Debug)] 231 | pub struct Error { 232 | inner: Context, 233 | } 234 | 235 | impl Error { 236 | /// Access the [`ErrorKind`] member. 237 | /// 238 | /// [`ErrorKind`]: enum.ErrorKind.html 239 | pub fn kind(&self) -> &ErrorKind { 240 | &*self.inner.get_context() 241 | } 242 | } 243 | 244 | impl From for Error { 245 | fn from(error_code: nghttp2_error) -> Error { 246 | let kind = match error_code { 247 | libnghttp2_sys::NGHTTP2_ERR_INVALID_ARGUMENT => { 248 | ErrorKind::InvalidArgument 249 | } 250 | libnghttp2_sys::NGHTTP2_ERR_BAD_CLIENT_MAGIC => ErrorKind::BadClientMagic, 251 | libnghttp2_sys::NGHTTP2_ERR_BUFFER_ERROR => ErrorKind::BufferError, 252 | libnghttp2_sys::NGHTTP2_ERR_CALLBACK_FAILURE => { 253 | ErrorKind::CallbackFailure 254 | } 255 | libnghttp2_sys::NGHTTP2_ERR_CANCEL => ErrorKind::Cancel, 256 | libnghttp2_sys::NGHTTP2_ERR_DATA_EXIST => ErrorKind::DataExist, 257 | libnghttp2_sys::NGHTTP2_ERR_DEFERRED => ErrorKind::Deferred, 258 | libnghttp2_sys::NGHTTP2_ERR_DEFERRED_DATA_EXIST => { 259 | ErrorKind::DeferredDataExist 260 | } 261 | libnghttp2_sys::NGHTTP2_ERR_EOF => ErrorKind::Eof, 262 | libnghttp2_sys::NGHTTP2_ERR_FATAL => ErrorKind::Fatal, 263 | libnghttp2_sys::NGHTTP2_ERR_FLOODED => ErrorKind::Flooded, 264 | libnghttp2_sys::NGHTTP2_ERR_FLOW_CONTROL => ErrorKind::FlowControl, 265 | libnghttp2_sys::NGHTTP2_ERR_FRAME_SIZE_ERROR => ErrorKind::FrameSizeError, 266 | libnghttp2_sys::NGHTTP2_ERR_GOAWAY_ALREADY_SENT => { 267 | ErrorKind::GoawayAlreadySent 268 | } 269 | libnghttp2_sys::NGHTTP2_ERR_HEADER_COMP => ErrorKind::HeaderComp, 270 | libnghttp2_sys::NGHTTP2_ERR_HTTP_HEADER => ErrorKind::HttpHeader, 271 | libnghttp2_sys::NGHTTP2_ERR_HTTP_MESSAGING => ErrorKind::HttpMessaging, 272 | libnghttp2_sys::NGHTTP2_ERR_INSUFF_BUFSIZE => ErrorKind::InsuffBufsize, 273 | libnghttp2_sys::NGHTTP2_ERR_INTERNAL => ErrorKind::Internal, 274 | libnghttp2_sys::NGHTTP2_ERR_INVALID_FRAME => ErrorKind::InvalidFrame, 275 | libnghttp2_sys::NGHTTP2_ERR_INVALID_HEADER_BLOCK => { 276 | ErrorKind::InvalidHeaderBlock 277 | } 278 | libnghttp2_sys::NGHTTP2_ERR_INVALID_STATE => ErrorKind::InvalidState, 279 | libnghttp2_sys::NGHTTP2_ERR_INVALID_STREAM_ID => { 280 | ErrorKind::InvalidStreamId 281 | } 282 | libnghttp2_sys::NGHTTP2_ERR_INVALID_STREAM_STATE => { 283 | ErrorKind::InvalidStreamState 284 | } 285 | libnghttp2_sys::NGHTTP2_ERR_NOMEM => ErrorKind::Nomem, 286 | libnghttp2_sys::NGHTTP2_ERR_PAUSE => ErrorKind::Pause, 287 | libnghttp2_sys::NGHTTP2_ERR_PROTO => ErrorKind::Proto, 288 | libnghttp2_sys::NGHTTP2_ERR_PUSH_DISABLED => ErrorKind::PushDisabled, 289 | libnghttp2_sys::NGHTTP2_ERR_REFUSED_STREAM => ErrorKind::RefusedStream, 290 | libnghttp2_sys::NGHTTP2_ERR_SESSION_CLOSING => ErrorKind::SessionClosing, 291 | libnghttp2_sys::NGHTTP2_ERR_SETTINGS_EXPECTED => { 292 | ErrorKind::SettingsExpected 293 | } 294 | libnghttp2_sys::NGHTTP2_ERR_START_STREAM_NOT_ALLOWED => { 295 | ErrorKind::StartStreamNotAllowed 296 | } 297 | libnghttp2_sys::NGHTTP2_ERR_STREAM_CLOSED => ErrorKind::StreamClosed, 298 | libnghttp2_sys::NGHTTP2_ERR_STREAM_CLOSING => ErrorKind::StreamClosing, 299 | libnghttp2_sys::NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE => { 300 | ErrorKind::StreamIdNotAvailable 301 | } 302 | libnghttp2_sys::NGHTTP2_ERR_STREAM_SHUT_WR => ErrorKind::StreamShutWr, 303 | libnghttp2_sys::NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE => { 304 | ErrorKind::TemporalCallbackFailure 305 | } 306 | libnghttp2_sys::NGHTTP2_ERR_TOO_MANY_INFLIGHT_SETTINGS => { 307 | ErrorKind::TooManyInflightSettings 308 | } 309 | libnghttp2_sys::NGHTTP2_ERR_UNSUPPORTED_VERSION => { 310 | ErrorKind::UnsupportedVersion 311 | } 312 | libnghttp2_sys::NGHTTP2_ERR_WOULDBLOCK => ErrorKind::WouldBlock, 313 | _ => unimplemented!(), 314 | }; 315 | let inner = Context::new(kind); 316 | Error { inner } 317 | } 318 | } 319 | 320 | impl Fail for Error { 321 | fn cause(&self) -> Option<&dyn Fail> { 322 | self.inner.cause() 323 | } 324 | 325 | fn backtrace(&self) -> Option<&Backtrace> { 326 | self.inner.backtrace() 327 | } 328 | } 329 | 330 | impl Display for Error { 331 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 332 | Display::fmt(&self.inner, f) 333 | } 334 | } 335 | 336 | impl From for Error { 337 | fn from(kind: ErrorKind) -> Error { 338 | let inner = Context::new(kind); 339 | Error { inner } 340 | } 341 | } 342 | 343 | impl From> for Error { 344 | fn from(inner: Context) -> Error { 345 | Error { inner } 346 | } 347 | } 348 | -------------------------------------------------------------------------------- /src/frames.rs: -------------------------------------------------------------------------------- 1 | //! HTTP/2 frames. 2 | 3 | mod data_frame; 4 | mod frame_flag; 5 | mod frame_header; 6 | mod frame_header_inflate_flags; 7 | mod frame_kind; 8 | mod headers_frame; 9 | mod settings_frame_kind; 10 | 11 | pub use crate::frames::data_frame::DataFrame; 12 | pub use crate::frames::frame_flag::FrameFlag; 13 | pub use crate::frames::frame_header::FrameHeader; 14 | pub use crate::frames::frame_header_inflate_flags::FrameHeaderInflateFlags; 15 | pub use crate::frames::frame_kind::FrameKind; 16 | pub use crate::frames::headers_frame::HeadersFrame; 17 | pub use crate::frames::settings_frame_kind::SettingsFrameKind; 18 | -------------------------------------------------------------------------------- /src/frames/data_frame.rs: -------------------------------------------------------------------------------- 1 | use super::FrameHeader; 2 | 3 | /// The DATA frame 4 | #[derive(Debug, Clone)] 5 | pub struct DataFrame { 6 | inner: libnghttp2_sys::nghttp2_data, 7 | } 8 | 9 | impl DataFrame { 10 | /// The length of the padding in this frame. This includes PAD_HIGH and PAD_LOW. 11 | #[inline] 12 | pub fn pad_len(&self) -> usize { 13 | self.inner.padlen 14 | } 15 | } 16 | 17 | impl FrameHeader for DataFrame { 18 | #[inline] 19 | fn header(&self) -> &libnghttp2_sys::nghttp2_frame_hd { 20 | &self.inner.hd 21 | } 22 | } 23 | 24 | #[doc(hidden)] 25 | impl From for DataFrame { 26 | #[inline] 27 | fn from(inner: libnghttp2_sys::nghttp2_data) -> Self { 28 | Self { inner } 29 | } 30 | } 31 | 32 | #[doc(hidden)] 33 | impl Into for DataFrame { 34 | #[inline] 35 | fn into(self) -> libnghttp2_sys::nghttp2_data { 36 | self.inner 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/frames/frame_flag.rs: -------------------------------------------------------------------------------- 1 | use bitflags::bitflags; 2 | 3 | bitflags! { 4 | /// The flags for HTTP/2 frames. This enum defines all flags for all frames. 5 | #[derive(Default)] 6 | pub struct FrameFlag: u32 { 7 | /// No flag set. 8 | const NONE = libnghttp2_sys::NGHTTP2_FLAG_NONE; 9 | 10 | /// The END_STREAM flag. 11 | const END_STREAM = libnghttp2_sys::NGHTTP2_FLAG_END_STREAM; 12 | 13 | /// The END_HEADERS flag. 14 | const END_HEADERS = libnghttp2_sys::NGHTTP2_FLAG_END_HEADERS; 15 | 16 | /// The ACK flag 17 | const ACK = libnghttp2_sys::NGHTTP2_FLAG_ACK; 18 | 19 | /// The PADDED flag. 20 | const PADDED = libnghttp2_sys::NGHTTP2_FLAG_PADDED; 21 | 22 | /// The PRIORITY flag. 23 | const PRIORITY = libnghttp2_sys::NGHTTP2_FLAG_PRIORITY; 24 | } 25 | } 26 | 27 | impl Into for FrameFlag { 28 | #[inline] 29 | fn into(self) -> u8 { 30 | self.bits as u8 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/frames/frame_header.rs: -------------------------------------------------------------------------------- 1 | use crate::StreamId; 2 | use libnghttp2_sys::nghttp2_frame_hd; 3 | 4 | /// Access the Header fields on a Frame. 5 | pub trait FrameHeader { 6 | /// Access the header directly. 7 | fn header(&self) -> &nghttp2_frame_hd; 8 | 9 | /// Get the flags from the Frame Header. 10 | /// 11 | /// TODO(yw): figure out which flags 12 | /// TODO(yw): create a flags type 13 | #[inline] 14 | fn flags(&self) -> u8 { 15 | self.header().flags 16 | } 17 | 18 | /// Access the stream identifier (aka, stream ID). 19 | #[inline] 20 | fn stream_id(&self) -> StreamId { 21 | self.header().stream_id 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/frames/frame_header_inflate_flags.rs: -------------------------------------------------------------------------------- 1 | use bitflags::bitflags; 2 | 3 | bitflags! { 4 | /// The flags for header inflation. 5 | #[derive(Default)] 6 | pub struct FrameHeaderInflateFlags: u32 { 7 | /// No flag set. 8 | const NONE = libnghttp2_sys::NGHTTP2_HD_INFLATE_NONE; 9 | 10 | /// Indicates all headers were inflated. 11 | const FINAL = libnghttp2_sys::NGHTTP2_HD_INFLATE_FINAL; 12 | 13 | /// Indicates a header was emitted. 14 | const EMIT = libnghttp2_sys::NGHTTP2_HD_INFLATE_EMIT; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/frames/frame_kind.rs: -------------------------------------------------------------------------------- 1 | use libnghttp2_sys::nghttp2_frame_type; 2 | use std::hint::unreachable_unchecked; 3 | 4 | /// The frame types in HTTP/2 specification. 5 | #[derive(Debug, Clone)] 6 | pub enum FrameKind { 7 | /// The DATA frame. 8 | Data, 9 | /// The HEADERS frame. 10 | Headers, 11 | /// The PRIORITY frame. 12 | Priority, 13 | /// The RST_STREAM frame. 14 | RstStream, 15 | /// The SETTINGS frame. 16 | Settings, 17 | /// The PUSH_PROMISE frame. 18 | PushPromise, 19 | /// The PING frame. 20 | Ping, 21 | /// The GOAWAY frame. 22 | Goaway, 23 | /// The WINDOW_UPDATE frame. 24 | WindowUpdate, 25 | /// The CONTINUATION frame. 26 | /// 27 | /// This frame type won't be passed to any 28 | /// callbacks because the library processes this frame type and its 29 | /// preceding HEADERS/PUSH_PROMISE as a single frame. 30 | Continuation, 31 | /// The ALTSVC frame, which is defined in [`RFC 7383`]. 32 | /// 33 | /// [`RFC 7383`]: https://tools.ietf.org/html/rfc7838#section-4. 34 | Altsvc, 35 | /// The ORIGIN frame, which is defined by [`RFC 8336`]. 36 | /// 37 | /// [`RFC 8336`]: https://tools.ietf.org/html/rfc8336. 38 | Origin, 39 | } 40 | 41 | #[doc(hidden)] 42 | impl From for FrameKind { 43 | #[inline] 44 | fn from(frame_type: nghttp2_frame_type) -> Self { 45 | match frame_type { 46 | libnghttp2_sys::NGHTTP2_DATA => FrameKind::Data, 47 | libnghttp2_sys::NGHTTP2_HEADERS => FrameKind::Headers, 48 | libnghttp2_sys::NGHTTP2_PRIORITY => FrameKind::Priority, 49 | libnghttp2_sys::NGHTTP2_RST_STREAM => FrameKind::RstStream, 50 | libnghttp2_sys::NGHTTP2_SETTINGS => FrameKind::Settings, 51 | libnghttp2_sys::NGHTTP2_PUSH_PROMISE => FrameKind::PushPromise, 52 | libnghttp2_sys::NGHTTP2_PING => FrameKind::Ping, 53 | libnghttp2_sys::NGHTTP2_GOAWAY => FrameKind::Goaway, 54 | libnghttp2_sys::NGHTTP2_WINDOW_UPDATE => FrameKind::WindowUpdate, 55 | libnghttp2_sys::NGHTTP2_CONTINUATION => FrameKind::Continuation, 56 | libnghttp2_sys::NGHTTP2_ALTSVC => FrameKind::Altsvc, 57 | libnghttp2_sys::NGHTTP2_ORIGIN => FrameKind::Origin, 58 | _ => unsafe { unreachable_unchecked() }, 59 | } 60 | } 61 | } 62 | 63 | #[doc(hidden)] 64 | impl Into for FrameKind { 65 | #[inline] 66 | fn into(self) -> nghttp2_frame_type { 67 | match self { 68 | FrameKind::Data => libnghttp2_sys::NGHTTP2_DATA, 69 | FrameKind::Headers => libnghttp2_sys::NGHTTP2_HEADERS, 70 | FrameKind::Priority => libnghttp2_sys::NGHTTP2_PRIORITY, 71 | FrameKind::RstStream => libnghttp2_sys::NGHTTP2_RST_STREAM, 72 | FrameKind::Settings => libnghttp2_sys::NGHTTP2_SETTINGS, 73 | FrameKind::PushPromise => libnghttp2_sys::NGHTTP2_PUSH_PROMISE, 74 | FrameKind::Ping => libnghttp2_sys::NGHTTP2_PING, 75 | FrameKind::Goaway => libnghttp2_sys::NGHTTP2_GOAWAY, 76 | FrameKind::WindowUpdate => libnghttp2_sys::NGHTTP2_WINDOW_UPDATE, 77 | FrameKind::Continuation => libnghttp2_sys::NGHTTP2_CONTINUATION, 78 | FrameKind::Altsvc => libnghttp2_sys::NGHTTP2_ALTSVC, 79 | FrameKind::Origin => libnghttp2_sys::NGHTTP2_ORIGIN, 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/frames/headers_frame.rs: -------------------------------------------------------------------------------- 1 | use super::FrameHeader; 2 | 3 | /// The HEADERS frame. 4 | #[derive(Debug, Clone)] 5 | pub struct HeadersFrame { 6 | inner: libnghttp2_sys::nghttp2_data, 7 | } 8 | 9 | impl HeadersFrame { 10 | /// The length of the padding in this frame. This includes PAD_HIGH and 11 | /// PAD_LOW. 12 | #[inline] 13 | pub fn pad_len(&self) -> usize { 14 | self.inner.padlen 15 | } 16 | 17 | /// TODO: docs 18 | pub fn priority_spec(&self) { 19 | unimplemented!(); 20 | } 21 | 22 | /// TODO: docs 23 | pub fn pairs(&self) { 24 | unimplemented!(); 25 | } 26 | 27 | /// TODO: docs 28 | pub fn category(&self) { 29 | unimplemented!(); 30 | } 31 | } 32 | 33 | impl FrameHeader for HeadersFrame { 34 | #[inline] 35 | fn header(&self) -> &libnghttp2_sys::nghttp2_frame_hd { 36 | &self.inner.hd 37 | } 38 | } 39 | 40 | #[doc(hidden)] 41 | impl From for HeadersFrame { 42 | #[inline] 43 | fn from(inner: libnghttp2_sys::nghttp2_data) -> Self { 44 | Self { inner } 45 | } 46 | } 47 | 48 | #[doc(hidden)] 49 | impl Into for HeadersFrame { 50 | #[inline] 51 | fn into(self) -> libnghttp2_sys::nghttp2_data { 52 | self.inner 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/frames/headers_kind.rs: -------------------------------------------------------------------------------- 1 | use libnghttp2_sys::nghttp2_headers_category; 2 | 3 | /// The category of HEADERS, which indicates the role of the frame. 4 | /// 5 | /// In HTTP/2 spec, request, response, push response and other arbitrary headers 6 | /// (e.g., trailer fields) are all called just HEADERS. To give the application 7 | /// the role of incoming HEADERS frame, we define several categories. 8 | pub enum HeadersKind { 9 | /// The HEADERS frame is opening new stream, which is analogous to SYN_STREAM 10 | /// in SPDY. 11 | Request, 12 | /// The HEADERS frame is the first response headers, which is analogous to 13 | /// SYN_REPLY in SPDY. 14 | Response, 15 | /// The HEADERS frame is the first headers sent against reserved stream. 16 | PushResponse, 17 | /// The HEADERS frame which does not apply for the above categories, which is 18 | /// analogous to HEADERS in SPDY. If non-final response (e.g., status 1xx) is 19 | /// used, final response HEADERS frame will be categorized here. 20 | Headers, 21 | } 22 | 23 | #[doc(hidden)] 24 | impl From for HeadersKind { 25 | #[inline] 26 | fn from(cat: nghttp2_headers_category) -> Self { 27 | match cat { 28 | libnghttp2_sys::NGHTTP2_HCAT_REQUEST => HeadersKind::Request, 29 | libnghttp2_sys::NGHTTP2_HCAT_RESPONSE => HeadersKind::Response, 30 | libnghttp2_sys::NGHTTP2_HCAT_PUSH_RESPONSE => HeadersKind::PushResponse, 31 | libnghttp2_sys::NGHTTP2_HCAT_HEADERS => HeadersKind::Headers, 32 | } 33 | } 34 | } 35 | 36 | #[doc(hidden)] 37 | impl Into for HeadersKind { 38 | #[inline] 39 | fn into(self) -> nghttp2_headers_category { 40 | match cat { 41 | HeadersKind::Request => libnghttp2_sys::NGHTTP2_HCAT_REQUEST, 42 | HeadersKind::Response => libnghttp2_sys::NGHTTP2_HCAT_RESPONSE, 43 | HeadersKind::PushResponse => libnghttp2_sys::NGHTTP2_HCAT_PUSH_RESPONSE, 44 | HeadersKind::Headers => libnghttp2_sys::NGHTTP2_HCAT_HEADERS, 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/frames/settings_frame_kind.rs: -------------------------------------------------------------------------------- 1 | use libnghttp2_sys::nghttp2_settings_id; 2 | use std::hint::unreachable_unchecked; 3 | 4 | /// Possible values of the `SETTINGS` frame. 5 | #[derive(Debug, Clone)] 6 | pub enum SettingsFrameKind { 7 | /// Table size. 8 | HeaderTableSize, 9 | /// Enable push. 10 | EnablePush, 11 | /// Max concurrent streams. 12 | MaxConcurrentStreams, 13 | /// Initial window size. 14 | InitialWindowSize, 15 | /// Max frame size. 16 | MaxFrameSize, 17 | /// Max header list size. 18 | MaxHeaderListSize, 19 | } 20 | 21 | #[doc(hidden)] 22 | impl From for SettingsFrameKind { 23 | #[inline] 24 | fn from(setting: nghttp2_settings_id) -> Self { 25 | match setting { 26 | libnghttp2_sys::NGHTTP2_SETTINGS_HEADER_TABLE_SIZE => { 27 | SettingsFrameKind::HeaderTableSize 28 | } 29 | libnghttp2_sys::NGHTTP2_SETTINGS_ENABLE_PUSH => { 30 | SettingsFrameKind::EnablePush 31 | } 32 | libnghttp2_sys::NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS => { 33 | SettingsFrameKind::MaxConcurrentStreams 34 | } 35 | libnghttp2_sys::NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE => { 36 | SettingsFrameKind::InitialWindowSize 37 | } 38 | libnghttp2_sys::NGHTTP2_SETTINGS_MAX_FRAME_SIZE => { 39 | SettingsFrameKind::MaxFrameSize 40 | } 41 | libnghttp2_sys::NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE => { 42 | SettingsFrameKind::MaxHeaderListSize 43 | } 44 | _ => unsafe { unreachable_unchecked() }, 45 | } 46 | } 47 | } 48 | 49 | #[doc(hidden)] 50 | impl Into for SettingsFrameKind { 51 | #[inline] 52 | fn into(self) -> nghttp2_settings_id { 53 | match self { 54 | SettingsFrameKind::HeaderTableSize => { 55 | libnghttp2_sys::NGHTTP2_SETTINGS_HEADER_TABLE_SIZE 56 | } 57 | SettingsFrameKind::EnablePush => { 58 | libnghttp2_sys::NGHTTP2_SETTINGS_ENABLE_PUSH 59 | } 60 | SettingsFrameKind::MaxConcurrentStreams => { 61 | libnghttp2_sys::NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS 62 | } 63 | SettingsFrameKind::InitialWindowSize => { 64 | libnghttp2_sys::NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE 65 | } 66 | SettingsFrameKind::MaxFrameSize => { 67 | libnghttp2_sys::NGHTTP2_SETTINGS_MAX_FRAME_SIZE 68 | } 69 | SettingsFrameKind::MaxHeaderListSize => { 70 | libnghttp2_sys::NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE 71 | } 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![forbid(future_incompatible)] 2 | #![deny(missing_debug_implementations, bad_style)] 3 | #![deny(missing_docs)] 4 | #![cfg_attr(test, deny(warnings))] 5 | 6 | //! ## Example 7 | //! ```rust 8 | //! ``` 9 | 10 | mod error; 11 | mod frames; 12 | mod name_value_flags; 13 | mod session; 14 | mod stream; 15 | 16 | /// The Settings identifier. 17 | pub type SettingsId = libnghttp2_sys::nghttp2_settings_id; 18 | 19 | pub use crate::error::*; 20 | pub use crate::frames::*; 21 | pub use crate::name_value_flags::*; 22 | pub use crate::session::*; 23 | pub use crate::stream::*; 24 | -------------------------------------------------------------------------------- /src/name_value_flags.rs: -------------------------------------------------------------------------------- 1 | use bitflags::bitflags; 2 | 3 | bitflags! { 4 | /// The flags for header field name/value pair. 5 | #[derive(Default)] 6 | pub struct NameValueFlags: u32 { 7 | /// No flag set. 8 | const NONE = libnghttp2_sys::NGHTTP2_NV_FLAG_NONE; 9 | 10 | /// Indicates that this name/value pair must not be indexed ("Literal 11 | /// Header Field never Indexed" representation must be used in HPACK 12 | /// encoding). Other implementation calls this bit as "sensitive". 13 | const NO_INDEX = libnghttp2_sys::NGHTTP2_NV_FLAG_NO_INDEX; 14 | 15 | /// This flag is set solely by application. If this flag is set, the 16 | /// library does not make a copy of header field name. This could 17 | /// improve performance. 18 | const NO_COPY_NAME = libnghttp2_sys::NGHTTP2_NV_FLAG_NO_COPY_NAME; 19 | 20 | /// This flag is set solely by application. If this flag is set, the 21 | /// library does not make a copy of header field value. This could 22 | /// improve performance. 23 | const NO_COPY_VALUE = libnghttp2_sys::NGHTTP2_NV_FLAG_NO_COPY_VALUE; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/session.rs: -------------------------------------------------------------------------------- 1 | use crate::error::Error; 2 | use crate::Stream; 3 | use crate::StreamId; 4 | use libnghttp2_sys::nghttp2_session; 5 | use crate::FrameFlag; 6 | 7 | /// Session type. 8 | #[derive(Debug)] 9 | pub struct Session { 10 | inner: nghttp2_session, 11 | } 12 | 13 | impl Session { 14 | /// Create a new instance. 15 | #[inline] 16 | pub fn new() -> Self { 17 | Self { 18 | inner: unsafe { std::mem::zeroed() }, 19 | } 20 | } 21 | 22 | /// Receives frames from the remote peer. 23 | /// 24 | /// ## Errors 25 | /// 26 | /// - NGHTTP2_ERR_EOF The remote peer did shutdown on the connection. 27 | /// - NGHTTP2_ERR_NOMEM Out of memory. 28 | /// - NGHTTP2_ERR_CALLBACK_FAILURE The callback function failed. 29 | /// - NGHTTP2_ERR_BAD_CLIENT_MAGIC Invalid client magic was detected. This 30 | /// error only returns when |session| was configured as server and 31 | /// nghttp2_option_set_no_recv_client_magic() is not used with nonzero 32 | /// value. 33 | /// - NGHTTP2_ERR_FLOODED Flooding was detected in this HTTP/2 session, and it 34 | /// must be closed. This is most likely caused by misbehaviour of peer. 35 | #[inline] 36 | pub fn receive(&mut self) -> Result<(), crate::error::Error> { 37 | let res = unsafe { libnghttp2_sys::nghttp2_session_recv(&mut self.inner) }; 38 | match res { 39 | 0 => Ok(()), 40 | n => Err(n.into()), 41 | } 42 | } 43 | 44 | /// Sends pending frames to the remote peer. 45 | /// 46 | /// ## Errors 47 | /// 48 | /// - NGHTTP2_ERR_NOMEM Out of memory. 49 | /// - NGHTTP2_ERR_CALLBACK_FAILURE The callback function failed. 50 | #[inline] 51 | pub fn send(&mut self) -> Result<(), crate::error::Error> { 52 | let res = unsafe { libnghttp2_sys::nghttp2_session_send(&mut self.inner) }; 53 | match res { 54 | 0 => Ok(()), 55 | n => Err(n.into()), 56 | } 57 | } 58 | 59 | /// Tell the `Session` that `size` bytes for `stream_id` were consumed, and 60 | /// are ready to `WINDOW_UPDATE`. The consumed bytes are counted towards both 61 | /// connection and stream level WINDOW_UPDATE. This function is intended to be 62 | /// used without automatic window update 63 | /// 64 | /// ## Errors 65 | /// 66 | /// :enum:NGHTTP2_ERR_NOMEM Out of memory. :enum:NGHTTP2_ERR_INVALID_ARGUMENT 67 | /// The |stream_id| is 0. :enum:NGHTTP2_ERR_INVALID_STATE Automatic 68 | /// WINDOW_UPDATE is not disabled. 69 | #[inline] 70 | pub fn consume( 71 | &mut self, 72 | stream_id: StreamId, 73 | size: usize, 74 | ) -> Result<(), crate::error::Error> { 75 | let res = unsafe { 76 | libnghttp2_sys::nghttp2_session_consume(&mut self.inner, stream_id, size) 77 | }; 78 | match res { 79 | 0 => Ok(()), 80 | n => Err(n.into()), 81 | } 82 | } 83 | 84 | /// Performs post-process of HTTP Upgrade request. This function can be called 85 | /// from both client and server, but the behavior is very different in each 86 | /// other. 87 | // TODO: implement Some branch for stream_user_data pointer 88 | // TODO: create variants for both client and server 89 | pub fn upgrade( 90 | &mut self, 91 | settings_payload: &[u8], 92 | is_head_request: bool, 93 | ) -> Result, crate::error::Error> { 94 | let head_request = match is_head_request { 95 | true => 0, 96 | false => 1, 97 | }; 98 | 99 | let stream_ptr = std::ptr::null_mut(); 100 | let payload_len = settings_payload.len(); 101 | let payload_ptr = settings_payload.as_ptr(); 102 | 103 | let res = unsafe { 104 | libnghttp2_sys::nghttp2_session_upgrade2( 105 | &mut self.inner, 106 | payload_ptr, 107 | payload_len, 108 | head_request, 109 | stream_ptr, 110 | ) 111 | }; 112 | match res { 113 | 0 => Ok(None), 114 | n => Err(n.into()), 115 | } 116 | } 117 | 118 | /// Puts back previously deferred DATA frame in the stream `stream_id` to the 119 | /// outbound queue. 120 | /// 121 | /// ## Errors 122 | /// 123 | /// NGHTTP2_ERR_INVALID_ARGUMENT The stream does not exist; 124 | #[inline] 125 | pub fn resume_data( 126 | &mut self, 127 | stream_id: StreamId, 128 | ) -> Result<(), crate::error::Error> { 129 | let res = unsafe { 130 | libnghttp2_sys::nghttp2_session_resume_data(&mut self.inner, stream_id) 131 | }; 132 | match res { 133 | 0 => Ok(()), 134 | n => Err(n.into()), 135 | } 136 | } 137 | 138 | /// Returns `true` if the session wants to receive data from the remote peer. 139 | #[inline] 140 | pub fn want_read(&mut self) -> bool { 141 | let res = 142 | unsafe { libnghttp2_sys::nghttp2_session_want_read(&mut self.inner) }; 143 | match res { 144 | 0 => false, 145 | _ => true, 146 | } 147 | } 148 | 149 | /// Returns `true` if the session wants to send data to the remote peer. 150 | #[inline] 151 | pub fn want_write(&mut self) -> bool { 152 | let res = 153 | unsafe { libnghttp2_sys::nghttp2_session_want_write(&mut self.inner) }; 154 | match res { 155 | 0 => false, 156 | _ => true, 157 | } 158 | } 159 | 160 | /// Returns the number of frames in the outbound queue. This does not include 161 | /// the deferred DATA frames. 162 | #[inline] 163 | pub fn get_outbound_queue_size(&mut self) -> usize { 164 | unsafe { 165 | libnghttp2_sys::nghttp2_session_get_outbound_queue_size(&mut self.inner) 166 | } 167 | } 168 | 169 | /// Submit a settings frame. 170 | pub fn submit_settings( 171 | &mut self, 172 | settings: &[libnghttp2_sys::nghttp2_settings_entry], 173 | ) -> Result<(), Error> { 174 | let flags = FrameFlag::NONE; // This is the only valid value. 175 | let settings_len = settings.len(); 176 | let settings_ptr = settings.as_ptr(); 177 | let res = unsafe { 178 | libnghttp2_sys::nghttp2_submit_settings(&mut self.inner, flags.into(), settings_ptr, settings_len) 179 | }; 180 | match res { 181 | 0 => Ok(()), 182 | n => Err(n.into()), 183 | } 184 | } 185 | } 186 | 187 | impl Drop for Session { 188 | #[inline] 189 | fn drop(&mut self) { 190 | unsafe { 191 | libnghttp2_sys::nghttp2_session_del(&mut self.inner); 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/stream.rs: -------------------------------------------------------------------------------- 1 | use libnghttp2_sys::nghttp2_stream; 2 | 3 | mod stream_exclusivity; 4 | mod stream_priority; 5 | mod stream_state; 6 | 7 | pub use crate::stream::stream_exclusivity::*; 8 | pub use crate::stream::stream_priority::*; 9 | pub use crate::stream::stream_state::*; 10 | 11 | /// The Stream identifier. 12 | pub type StreamId = i32; 13 | 14 | /// The Stream struct. 15 | #[derive(Debug)] 16 | pub struct Stream { 17 | inner: nghttp2_stream, 18 | } 19 | 20 | impl Stream { 21 | /// Create a new instance. 22 | #[inline] 23 | pub fn new() -> Self { 24 | Self { 25 | inner: unsafe { std::mem::zeroed() }, 26 | } 27 | } 28 | } 29 | 30 | #[doc(hidden)] 31 | impl From for Stream { 32 | #[inline] 33 | fn from(inner: libnghttp2_sys::nghttp2_stream) -> Self { 34 | Self { inner } 35 | } 36 | } 37 | 38 | #[doc(hidden)] 39 | impl Into for Stream { 40 | #[inline] 41 | fn into(self) -> libnghttp2_sys::nghttp2_stream { 42 | self.inner 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/stream/stream_exclusivity.rs: -------------------------------------------------------------------------------- 1 | use std::num::NonZeroU8; 2 | 3 | /// Determine the exclusivity of a stream. 4 | #[derive(Debug, Clone)] 5 | pub enum StreamExclusivity { 6 | /// The stream is an exclusive dependency. 7 | Exclusive, 8 | /// The stream is a non-exclusive dependency. 9 | Inclusive(NonZeroU8), 10 | } 11 | 12 | impl StreamExclusivity { 13 | /// Create an instance from a `u8`. 14 | #[inline] 15 | pub fn from_u8(n: u8) -> Self { 16 | match n { 17 | 0 => StreamExclusivity::Exclusive, 18 | n => { 19 | let n = unsafe { NonZeroU8::new_unchecked(n) }; 20 | StreamExclusivity::Inclusive(n) 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/stream/stream_priority.rs: -------------------------------------------------------------------------------- 1 | use super::StreamExclusivity; 2 | use super::StreamId; 3 | use libnghttp2_sys::nghttp2_priority_spec; 4 | 5 | /// The structure to specify stream dependency. 6 | #[derive(Debug, Clone)] 7 | pub struct StreamPriority { 8 | inner: nghttp2_priority_spec, 9 | } 10 | 11 | impl StreamPriority { 12 | /// Get the Stream ID. 13 | #[inline] 14 | pub fn stream_id(&self) -> StreamId { 15 | self.inner.stream_id 16 | } 17 | 18 | /// The weight of this dependency. 19 | #[inline] 20 | pub fn weight(&self) -> i32 { 21 | self.inner.weight 22 | } 23 | 24 | /// The weight of this dependency. 25 | #[inline] 26 | pub fn exclusivity(&self) -> StreamExclusivity { 27 | StreamExclusivity::from_u8(self.inner.exclusive) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/stream/stream_state.rs: -------------------------------------------------------------------------------- 1 | use libnghttp2_sys::nghttp2_stream_proto_state; 2 | use std::hint::unreachable_unchecked; 3 | 4 | /// State of stream as described in RFC 7540. 5 | #[derive(Debug, Clone)] 6 | pub enum StreamState { 7 | /// idle state. 8 | Idle, 9 | /// open state. 10 | Open, 11 | /// reserved (local) state. 12 | ReservedLocal, 13 | /// reserved (remote) state. 14 | ReservedRemote, 15 | /// half closed (local) state. 16 | HalfClosedLocal, 17 | /// half closed (remote) state. 18 | HalfClosedRemote, 19 | /// closed state. 20 | Closed, 21 | } 22 | 23 | #[doc(hidden)] 24 | impl From for StreamState { 25 | #[inline] 26 | fn from(from: nghttp2_stream_proto_state) -> Self { 27 | match from { 28 | libnghttp2_sys::NGHTTP2_STREAM_STATE_IDLE => StreamState::Idle, 29 | libnghttp2_sys::NGHTTP2_STREAM_STATE_OPEN => StreamState::Open, 30 | libnghttp2_sys::NGHTTP2_STREAM_STATE_RESERVED_LOCAL => { 31 | StreamState::ReservedLocal 32 | } 33 | libnghttp2_sys::NGHTTP2_STREAM_STATE_RESERVED_REMOTE => { 34 | StreamState::ReservedRemote 35 | } 36 | libnghttp2_sys::NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL => { 37 | StreamState::HalfClosedLocal 38 | } 39 | libnghttp2_sys::NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE => { 40 | StreamState::HalfClosedRemote 41 | } 42 | libnghttp2_sys::NGHTTP2_STREAM_STATE_CLOSED => StreamState::Closed, 43 | _ => unsafe { unreachable_unchecked() }, 44 | } 45 | } 46 | } 47 | 48 | #[doc(hidden)] 49 | impl Into for StreamState { 50 | #[inline] 51 | fn into(self) -> nghttp2_stream_proto_state { 52 | match self { 53 | StreamState::Idle => libnghttp2_sys::NGHTTP2_STREAM_STATE_IDLE, 54 | StreamState::Open => libnghttp2_sys::NGHTTP2_STREAM_STATE_OPEN, 55 | StreamState::ReservedLocal => { 56 | libnghttp2_sys::NGHTTP2_STREAM_STATE_RESERVED_LOCAL 57 | } 58 | StreamState::ReservedRemote => { 59 | libnghttp2_sys::NGHTTP2_STREAM_STATE_RESERVED_REMOTE 60 | } 61 | StreamState::HalfClosedLocal => { 62 | libnghttp2_sys::NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL 63 | } 64 | StreamState::HalfClosedRemote => { 65 | libnghttp2_sys::NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE 66 | } 67 | StreamState::Closed => libnghttp2_sys::NGHTTP2_STREAM_STATE_CLOSED, 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /tests/test.rs: -------------------------------------------------------------------------------- 1 | extern crate failure; 2 | extern crate nghttp2; 3 | 4 | use failure::Error; 5 | 6 | #[test] 7 | fn should_work() -> Result<(), Error> { 8 | Ok(()) 9 | } 10 | --------------------------------------------------------------------------------