├── .travis.yml ├── LICENSE ├── README.md ├── config ├── ngx_fixed_buffer_cache.c ├── ngx_fixed_buffer_cache.h ├── ngx_fixed_buffer_cache_internal.h ├── ngx_http_parallel_module.c └── travis_build.sh /.travis.yml: -------------------------------------------------------------------------------- 1 | before_install: 2 | - sudo apt-get update -qq 3 | - sudo apt-get install -y libssl-dev libpcre3-dev wget unzip 4 | language: c 5 | compiler: 6 | - clang 7 | - gcc 8 | script: ./travis_build.sh 9 | notifications: 10 | email: 11 | recipients: 12 | - eran.kornblau@kaltura.com 13 | - jess.portnoy@kaltura.com 14 | on_success: change 15 | on_failure: always 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published by 637 | the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Parallel module for Nginx [![Build Status](https://travis-ci.org/kaltura/nginx-parallel-module.svg?branch=master)](https://travis-ci.org/kaltura/nginx-parallel-module) 2 | 3 | Reads data from an upstream-backed nginx location by running several concurrent range requests. 4 | 5 | ## Installation 6 | 7 | Add `--add-module` when configuring nginx: 8 | 9 | ./configure --add-module=$PATH_TO_PARALLEL_MODULE 10 | 11 | ## Configuration 12 | 13 | ### Directives 14 | 15 | #### parallel 16 | * **syntax**: `parallel uri_prefix` 17 | * **default**: `none` 18 | * **context**: `location` 19 | 20 | Enables the parallel module on the enclosing location, the uri_prefix is added to the request URI 21 | to form the URI of the subrequests 22 | 23 | #### parallel_fiber_count 24 | * **syntax**: `parallel_fiber_count count` 25 | * **default**: `8` 26 | * **context**: `http`, `server`, `location` 27 | 28 | Sets the maximum number of concurrent subrequests 29 | 30 | #### parallel_min_chunk_size 31 | * **syntax**: `parallel_min_chunk_size size` 32 | * **default**: `128k` 33 | * **context**: `http`, `server`, `location` 34 | 35 | Sets the minimum size that is requested in a single subrequest, only the request for the last chunk 36 | is allowed to return less than this value. This parameter also controls the size that is requested in 37 | the initial subrequests, that are issued before the response size is known. 38 | 39 | #### parallel_max_chunk_size 40 | * **syntax**: `parallel_max_chunk_size size` 41 | * **default**: `512k` 42 | * **context**: `http`, `server`, `location` 43 | 44 | Sets the maximum size that can be fetched in a single subrequest 45 | 46 | #### parallel_max_headers_size 47 | * **syntax**: `parallel_max_headers_size size` 48 | * **default**: `4k` 49 | * **context**: `http`, `server`, `location` 50 | 51 | Sets the size that should be allocated for holding the subrequests headers 52 | 53 | #### parallel_max_buffer_count 54 | * **syntax**: `parallel_max_buffer_count count` 55 | * **default**: `16` 56 | * **context**: `http`, `server`, `location` 57 | 58 | Sets the maximum number of buffers that the module is allowed to allocate. 59 | Once the limit is reached, the module will until data is flushed to the client before issuing 60 | more subrequests. 61 | 62 | #### parallel_consistency_check_etag 63 | * **syntax**: `parallel_consistency_check_etag on/off` 64 | * **default**: `on` 65 | * **context**: `http`, `server`, `location` 66 | 67 | Enables/disables ETag consistency validation, when enabled, the server will make sure all ETag headers 68 | returned for subrequests are consistent and fail the request otherwise. 69 | 70 | #### parallel_consistency_check_last_modified 71 | * **syntax**: `parallel_consistency_check_last_modified on/off` 72 | * **default**: `on` 73 | * **context**: `http`, `server`, `location` 74 | 75 | Enables/disables Last-Modified consistency validation, when enabled, the server will make sure all Last-Modified headers 76 | returned for subrequests are consistent and fail the request otherwise. 77 | 78 | #### parallel_content_length_cache 79 | * **syntax**: `parallel_content_length_cache zone_name zone_size` 80 | * **default**: `off` 81 | * **context**: `http`, `server`, `location` 82 | 83 | Configures the size and shared memory object name of the content length cache. 84 | This cache holds the content lengths of requested URLs in order to optimize their retrieval in the future. 85 | Cached lengths serve as a hint to the module when making the initial batch of subrequests. 86 | If the content length is large, a larger chunk size can be used, if the content length is small, 87 | the module can avoid making subrequests that will fail with 416 error. 88 | An incorrect length that is saved in the cache can not affect the response generated by this module, 89 | but it can lead to a less efficient pull from the origin. 90 | Each saved content length record consumes about 80 bytes, so a cache of few hundred megabytes can probably last 91 | for a long time. 92 | 93 | ### Sample configuration 94 | 95 | http { 96 | 97 | upstream backend { 98 | server backendhost:80; 99 | keepalive 32; 100 | } 101 | 102 | server { 103 | 104 | location /parallel { 105 | parallel /proxy; 106 | } 107 | 108 | location /proxy/parallel/ { 109 | proxy_pass http://backend/; 110 | proxy_http_version 1.1; 111 | proxy_set_header Connection ""; 112 | proxy_set_header Host $http_host; 113 | 114 | internal; 115 | } 116 | } 117 | } 118 | 119 | ## Copyright & License 120 | 121 | All code in this project is released under the [AGPLv3 license](http://www.gnu.org/licenses/agpl-3.0.html) unless a different license for a particular library is specified in the applicable library path. 122 | 123 | Copyright © Kaltura Inc. All rights reserved. 124 | -------------------------------------------------------------------------------- /config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_parallel_module 2 | 3 | HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_parallel_module" 4 | 5 | NGX_ADDON_DEPS="$NGX_ADDON_DEPS \ 6 | $ngx_addon_dir/ngx_fixed_buffer_cache.h \ 7 | $ngx_addon_dir/ngx_fixed_buffer_cache_internal.h \ 8 | " 9 | 10 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS \ 11 | $ngx_addon_dir/ngx_fixed_buffer_cache.c \ 12 | $ngx_addon_dir/ngx_http_parallel_module.c \ 13 | " 14 | -------------------------------------------------------------------------------- /ngx_fixed_buffer_cache.c: -------------------------------------------------------------------------------- 1 | #include "ngx_fixed_buffer_cache_internal.h" 2 | 3 | #define ZONE_NAME_FORMAT "%uz-%V" 4 | 5 | // Note: code taken from ngx_str_rbtree_insert_value, updated the node comparison 6 | static void 7 | ngx_fixed_buffer_cache_rbtree_insert_value( 8 | ngx_rbtree_node_t *temp, 9 | ngx_rbtree_node_t *node, 10 | ngx_rbtree_node_t *sentinel) 11 | { 12 | ngx_fixed_buffer_cache_entry_t *n, *t; 13 | ngx_rbtree_node_t **p; 14 | 15 | for (;;) 16 | { 17 | n = (ngx_fixed_buffer_cache_entry_t *)node; 18 | t = (ngx_fixed_buffer_cache_entry_t *)temp; 19 | 20 | if (node->key != temp->key) 21 | { 22 | p = (node->key < temp->key) ? &temp->left : &temp->right; 23 | } 24 | else 25 | { 26 | p = (ngx_memcmp(n->key, t->key, NGX_FIXED_BUFFER_CACHE_KEY_SIZE) < 0) 27 | ? &temp->left : &temp->right; 28 | } 29 | 30 | if (*p == sentinel) 31 | { 32 | break; 33 | } 34 | 35 | temp = *p; 36 | } 37 | 38 | *p = node; 39 | node->parent = temp; 40 | node->left = sentinel; 41 | node->right = sentinel; 42 | ngx_rbt_red(node); 43 | } 44 | 45 | // Note: code taken from ngx_str_rbtree_lookup, updated the node comparison 46 | static ngx_fixed_buffer_cache_entry_t * 47 | ngx_fixed_buffer_cache_rbtree_lookup(ngx_rbtree_t *rbtree, const u_char* key, uint32_t hash) 48 | { 49 | ngx_fixed_buffer_cache_entry_t *n; 50 | ngx_rbtree_node_t *node, *sentinel; 51 | ngx_int_t rc; 52 | 53 | node = rbtree->root; 54 | sentinel = rbtree->sentinel; 55 | 56 | while (node != sentinel) 57 | { 58 | n = (ngx_fixed_buffer_cache_entry_t *)node; 59 | 60 | if (hash != node->key) 61 | { 62 | node = (hash < node->key) ? node->left : node->right; 63 | continue; 64 | } 65 | 66 | rc = ngx_memcmp(key, n->key, NGX_FIXED_BUFFER_CACHE_KEY_SIZE); 67 | if (rc < 0) 68 | { 69 | node = node->left; 70 | continue; 71 | } 72 | 73 | if (rc > 0) 74 | { 75 | node = node->right; 76 | continue; 77 | } 78 | 79 | return n; 80 | } 81 | 82 | return NULL; 83 | } 84 | 85 | static void 86 | ngx_fixed_buffer_cache_reset(ngx_fixed_buffer_cache_t *cache) 87 | { 88 | cache->entries_pos = cache->entries_start; 89 | ngx_rbtree_init(&cache->rbtree, &cache->sentinel, ngx_fixed_buffer_cache_rbtree_insert_value); 90 | ngx_queue_init(&cache->used_queue); 91 | 92 | // update stats (everything is evicted) 93 | cache->stats.evicted = cache->stats.store_ok; 94 | } 95 | 96 | static ngx_int_t 97 | ngx_fixed_buffer_cache_init(ngx_shm_zone_t *shm_zone, void *data) 98 | { 99 | ngx_fixed_buffer_cache_t *cache; 100 | ngx_slab_pool_t *shpool; 101 | ssize_t buffer_size; 102 | u_char* dash_pos; 103 | u_char* p; 104 | 105 | if (data) 106 | { 107 | shm_zone->data = data; 108 | return NGX_OK; 109 | } 110 | 111 | shpool = (ngx_slab_pool_t *)shm_zone->shm.addr; 112 | 113 | if (shm_zone->shm.exists) 114 | { 115 | shm_zone->data = shpool->data; 116 | return NGX_OK; 117 | } 118 | 119 | // start following the ngx_slab_pool_t that was allocated at the beginning of the chunk 120 | p = shm_zone->shm.addr + sizeof(ngx_slab_pool_t); 121 | 122 | // initialize the log context 123 | shpool->log_ctx = p; 124 | p = ngx_sprintf(shpool->log_ctx, " in buffer cache \"%V\"%Z", &shm_zone->shm.name); 125 | 126 | // allocate the cache state 127 | cache = (ngx_fixed_buffer_cache_t*)p; 128 | p += sizeof(*cache); 129 | 130 | // initialize fixed cache fields 131 | cache->entries_start = p; 132 | cache->entries_end = shm_zone->shm.addr + shm_zone->shm.size; 133 | 134 | // parse the buffer size from the shm name 135 | dash_pos = memchr(shm_zone->shm.name.data, '-', shm_zone->shm.name.len); 136 | if (dash_pos == NULL) 137 | { 138 | return NGX_ERROR; // unexpected 139 | } 140 | 141 | buffer_size = ngx_atosz(shm_zone->shm.name.data, dash_pos - shm_zone->shm.name.data); 142 | if (buffer_size < 0) 143 | { 144 | return NGX_ERROR; // unexpected 145 | } 146 | cache->buffer_size = buffer_size; 147 | 148 | // reset the stats 149 | ngx_memzero(&cache->stats, sizeof(cache->stats)); 150 | 151 | // reset the cache status 152 | ngx_fixed_buffer_cache_reset(cache); 153 | cache->reset = 0; 154 | 155 | // set the cache struct as the data of the shared pool 156 | shpool->data = cache; 157 | 158 | return NGX_OK; 159 | } 160 | 161 | /* Note: must be called with the mutex locked */ 162 | static ngx_fixed_buffer_cache_entry_t* 163 | ngx_fixed_buffer_cache_get_free_entry(ngx_fixed_buffer_cache_t *cache) 164 | { 165 | ngx_fixed_buffer_cache_entry_t* entry; 166 | u_char* next_entries_pos; 167 | 168 | next_entries_pos = cache->entries_pos + sizeof(*entry) + cache->buffer_size; 169 | if (next_entries_pos <= cache->entries_end) 170 | { 171 | // enlarge the entries buffer 172 | entry = (ngx_fixed_buffer_cache_entry_t*)cache->entries_pos; 173 | cache->entries_pos = next_entries_pos; 174 | return entry; 175 | } 176 | 177 | // verify we have an entry to free 178 | if (ngx_queue_empty(&cache->used_queue)) 179 | { 180 | return NULL; 181 | } 182 | 183 | // get oldest entry 184 | entry = container_of(ngx_queue_head(&cache->used_queue), ngx_fixed_buffer_cache_entry_t, queue_node); 185 | 186 | // remove from rb tree and used queue 187 | ngx_queue_remove(&entry->queue_node); 188 | ngx_rbtree_delete(&cache->rbtree, &entry->node); 189 | 190 | // update stats 191 | cache->stats.evicted++; 192 | 193 | return entry; 194 | } 195 | 196 | ngx_flag_t 197 | ngx_fixed_buffer_cache_fetch( 198 | ngx_shm_zone_t *shm_zone, 199 | u_char* key, 200 | u_char* buffer) 201 | { 202 | ngx_fixed_buffer_cache_entry_t* entry; 203 | ngx_fixed_buffer_cache_t *cache; 204 | ngx_slab_pool_t *shpool; 205 | ngx_flag_t result = 0; 206 | uint32_t hash; 207 | 208 | hash = ngx_crc32_short(key, NGX_FIXED_BUFFER_CACHE_KEY_SIZE); 209 | 210 | shpool = (ngx_slab_pool_t *)shm_zone->shm.addr; 211 | cache = shpool->data; 212 | 213 | ngx_shmtx_lock(&shpool->mutex); 214 | 215 | if (!cache->reset) 216 | { 217 | entry = ngx_fixed_buffer_cache_rbtree_lookup(&cache->rbtree, key, hash); 218 | if (entry != NULL) 219 | { 220 | result = 1; 221 | 222 | // copy buffer 223 | ngx_memcpy(buffer, entry + 1, cache->buffer_size); 224 | 225 | // update stats 226 | cache->stats.fetch_hit++; 227 | } 228 | else 229 | { 230 | // update stats 231 | cache->stats.fetch_miss++; 232 | } 233 | } 234 | 235 | ngx_shmtx_unlock(&shpool->mutex); 236 | 237 | return result; 238 | } 239 | 240 | ngx_flag_t 241 | ngx_fixed_buffer_cache_store( 242 | ngx_shm_zone_t *shm_zone, 243 | u_char* key, 244 | u_char* buffer, 245 | ngx_flag_t overwrite) 246 | { 247 | ngx_fixed_buffer_cache_entry_t* entry; 248 | ngx_fixed_buffer_cache_t *cache; 249 | ngx_slab_pool_t *shpool; 250 | uint32_t hash; 251 | 252 | hash = ngx_crc32_short(key, NGX_FIXED_BUFFER_CACHE_KEY_SIZE); 253 | 254 | shpool = (ngx_slab_pool_t *)shm_zone->shm.addr; 255 | cache = shpool->data; 256 | 257 | ngx_shmtx_lock(&shpool->mutex); 258 | 259 | if (cache->reset) 260 | { 261 | // reset the cache, leave the reset flag enabled 262 | ngx_fixed_buffer_cache_reset(cache); 263 | 264 | // update stats 265 | cache->stats.reset++; 266 | } 267 | else 268 | { 269 | // check whether the entry already exists 270 | entry = ngx_fixed_buffer_cache_rbtree_lookup(&cache->rbtree, key, hash); 271 | if (entry != NULL) 272 | { 273 | if (overwrite) 274 | { 275 | cache->reset = 1; 276 | 277 | // push to the end of the used queue 278 | ngx_queue_remove(&entry->queue_node); 279 | ngx_queue_insert_tail(&cache->used_queue, &entry->queue_node); 280 | 281 | // update buffer 282 | memcpy(entry + 1, buffer, cache->buffer_size); 283 | 284 | cache->reset = 0; 285 | 286 | // update stats 287 | cache->stats.store_ok++; 288 | } 289 | else 290 | { 291 | // update stats 292 | cache->stats.store_exists++; 293 | } 294 | ngx_shmtx_unlock(&shpool->mutex); 295 | return overwrite; 296 | } 297 | 298 | // enable the reset flag before we start making any changes 299 | cache->reset = 1; 300 | } 301 | 302 | // allocate a new entry 303 | entry = ngx_fixed_buffer_cache_get_free_entry(cache); 304 | if (entry == NULL) 305 | { 306 | cache->reset = 0; 307 | cache->stats.store_err++; 308 | ngx_shmtx_unlock(&shpool->mutex); 309 | return 0; 310 | } 311 | 312 | // initialize the entry 313 | entry->node.key = hash; 314 | memcpy(entry->key, key, NGX_FIXED_BUFFER_CACHE_KEY_SIZE); 315 | 316 | // add to rbtree and used queue 317 | ngx_rbtree_insert(&cache->rbtree, &entry->node); 318 | ngx_queue_insert_tail(&cache->used_queue, &entry->queue_node); 319 | 320 | // copy the buffer 321 | memcpy(entry + 1, buffer, cache->buffer_size); 322 | 323 | cache->reset = 0; 324 | 325 | // update stats 326 | cache->stats.store_ok++; 327 | 328 | ngx_shmtx_unlock(&shpool->mutex); 329 | 330 | return 1; 331 | } 332 | 333 | void 334 | ngx_fixed_buffer_cache_get_stats( 335 | ngx_shm_zone_t *shm_zone, 336 | ngx_fixed_buffer_cache_stats_t* stats) 337 | { 338 | ngx_fixed_buffer_cache_t *cache; 339 | ngx_slab_pool_t *shpool; 340 | 341 | shpool = (ngx_slab_pool_t *)shm_zone->shm.addr; 342 | cache = shpool->data; 343 | 344 | ngx_shmtx_lock(&shpool->mutex); 345 | 346 | memcpy(stats, &cache->stats, sizeof(cache->stats)); 347 | 348 | stats->entries = (cache->entries_pos - cache->entries_start) / (sizeof(ngx_fixed_buffer_cache_entry_t) + cache->buffer_size); 349 | 350 | ngx_shmtx_unlock(&shpool->mutex); 351 | } 352 | 353 | void 354 | ngx_fixed_buffer_cache_reset_stats(ngx_shm_zone_t *shm_zone) 355 | { 356 | ngx_fixed_buffer_cache_t *cache; 357 | ngx_slab_pool_t *shpool; 358 | 359 | shpool = (ngx_slab_pool_t *)shm_zone->shm.addr; 360 | cache = shpool->data; 361 | 362 | ngx_shmtx_lock(&shpool->mutex); 363 | 364 | ngx_memzero(&cache->stats, sizeof(cache->stats)); 365 | 366 | ngx_shmtx_unlock(&shpool->mutex); 367 | } 368 | 369 | ngx_shm_zone_t* 370 | ngx_fixed_buffer_cache_create_zone( 371 | ngx_conf_t *cf, 372 | ngx_str_t *name, 373 | size_t size, 374 | size_t buffer_size, 375 | void *tag) 376 | { 377 | ngx_shm_zone_t* result; 378 | ngx_str_t updated_name; 379 | 380 | // encode the buffer size on the zone name (no way to pass context to the init function) 381 | updated_name.data = ngx_pnalloc(cf->pool, sizeof(ZONE_NAME_FORMAT) + name->len + NGX_SIZE_T_LEN); 382 | if (updated_name.data == NULL) 383 | { 384 | return NULL; 385 | } 386 | updated_name.len = ngx_sprintf(updated_name.data, ZONE_NAME_FORMAT, buffer_size, name) - updated_name.data; 387 | 388 | // create the shared memory 389 | result = ngx_shared_memory_add(cf, &updated_name, size, tag); 390 | if (result == NULL) 391 | { 392 | return NULL; 393 | } 394 | 395 | result->init = ngx_fixed_buffer_cache_init; 396 | return result; 397 | } 398 | -------------------------------------------------------------------------------- /ngx_fixed_buffer_cache.h: -------------------------------------------------------------------------------- 1 | #ifndef _NGX_FIXED_BUFFER_CACHE_H_INCLUDED_ 2 | #define _NGX_FIXED_BUFFER_CACHE_H_INCLUDED_ 3 | 4 | // includes 5 | #include 6 | 7 | // constants 8 | #define NGX_FIXED_BUFFER_CACHE_KEY_SIZE (16) 9 | 10 | // typedefs 11 | typedef struct { 12 | ngx_atomic_t store_ok; 13 | ngx_atomic_t store_err; 14 | ngx_atomic_t store_exists; 15 | ngx_atomic_t fetch_hit; 16 | ngx_atomic_t fetch_miss; 17 | ngx_atomic_t evicted; 18 | ngx_atomic_t reset; 19 | 20 | // updated only when the stats are fetched 21 | ngx_atomic_t entries; 22 | } ngx_fixed_buffer_cache_stats_t; 23 | 24 | // functions 25 | ngx_flag_t ngx_fixed_buffer_cache_fetch( 26 | ngx_shm_zone_t *shm_zone, 27 | u_char* key, 28 | u_char* buffer); 29 | 30 | ngx_flag_t ngx_fixed_buffer_cache_store( 31 | ngx_shm_zone_t *shm_zone, 32 | u_char* key, 33 | u_char* buffer, 34 | ngx_flag_t overwrite); 35 | 36 | void ngx_fixed_buffer_cache_get_stats( 37 | ngx_shm_zone_t *shm_zone, 38 | ngx_fixed_buffer_cache_stats_t* stats); 39 | 40 | void ngx_fixed_buffer_cache_reset_stats(ngx_shm_zone_t *shm_zone); 41 | 42 | ngx_shm_zone_t* ngx_fixed_buffer_cache_create_zone( 43 | ngx_conf_t *cf, 44 | ngx_str_t *name, 45 | size_t size, 46 | size_t buffer_size, 47 | void *tag); 48 | 49 | #endif // _NGX_FIXED_BUFFER_CACHE_H_INCLUDED_ -------------------------------------------------------------------------------- /ngx_fixed_buffer_cache_internal.h: -------------------------------------------------------------------------------- 1 | #ifndef _ngx_fixed_buffer_cache_INTERNAL_H_INCLUDED_ 2 | #define _ngx_fixed_buffer_cache_INTERNAL_H_INCLUDED_ 3 | 4 | #include "ngx_fixed_buffer_cache.h" 5 | #include "ngx_queue.h" 6 | 7 | // macros 8 | #ifndef container_of 9 | #define container_of(ptr, type, member) (type *)((char *)(ptr) - offsetof(type, member)) 10 | #endif 11 | 12 | // typedefs 13 | typedef struct { 14 | ngx_rbtree_node_t node; 15 | ngx_queue_t queue_node; 16 | u_char key[NGX_FIXED_BUFFER_CACHE_KEY_SIZE]; 17 | } ngx_fixed_buffer_cache_entry_t; 18 | 19 | typedef struct { 20 | ngx_atomic_t reset; 21 | ngx_rbtree_t rbtree; 22 | ngx_rbtree_node_t sentinel; 23 | ngx_queue_t used_queue; 24 | size_t buffer_size; 25 | u_char* entries_start; 26 | u_char* entries_pos; 27 | u_char* entries_end; 28 | ngx_fixed_buffer_cache_stats_t stats; 29 | } ngx_fixed_buffer_cache_t; 30 | 31 | #endif // _ngx_fixed_buffer_cache_INTERNAL_H_INCLUDED_ 32 | -------------------------------------------------------------------------------- /ngx_http_parallel_module.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "ngx_fixed_buffer_cache.h" 8 | 9 | // macros 10 | #define DIV_CEIL(nom, denom) (((nom) + (denom) - 1) / (denom)) 11 | 12 | #define RANGE_FORMAT "bytes=%O-%O" 13 | #define CONTENT_RANGE_FORMAT "bytes %O-%O/%O" 14 | 15 | // typedefs 16 | typedef struct { 17 | off_t start; 18 | off_t end; 19 | ngx_flag_t is_range_request; 20 | } ngx_http_range_t; 21 | 22 | typedef struct { 23 | ngx_str_t uri_prefix; 24 | ngx_uint_t fiber_count; 25 | size_t min_chunk_size; 26 | size_t max_chunk_size; 27 | size_t max_headers_size; 28 | ngx_uint_t max_buffer_count; 29 | ngx_flag_t consistency_check_etag; 30 | ngx_flag_t consistency_check_last_modified; 31 | ngx_shm_zone_t* content_length_cache_zone; 32 | } ngx_http_parallel_loc_conf_t; 33 | 34 | typedef struct ngx_http_parallel_fiber_ctx_s { 35 | ngx_http_headers_in_t headers_in; 36 | ngx_list_t upstream_headers; 37 | ngx_http_post_subrequest_t *psr; 38 | uint64_t chunk_index; 39 | ngx_http_request_t *sr; 40 | ngx_chain_t* cl; 41 | struct ngx_http_parallel_fiber_ctx_s* next; 42 | } ngx_http_parallel_fiber_ctx_t; 43 | 44 | typedef struct { 45 | ngx_str_t sr_uri; 46 | ngx_http_headers_in_t original_headers_in; 47 | ngx_http_range_t range; 48 | ngx_uint_t fiber_count; // fiber_count == 1 means that the request is proxied as is 49 | off_t cached_response_length; 50 | 51 | ngx_flag_t key_inited; 52 | u_char key[NGX_FIXED_BUFFER_CACHE_KEY_SIZE]; 53 | 54 | ngx_chain_t** chunks; 55 | uint64_t chunk_count; 56 | uint64_t missing_chunks; // a bitmask of chunks that returned 416 57 | uint64_t next_send_chunk; // next chunk to send to the client 58 | uint64_t next_request_chunk; // next chunk to request from upstream 59 | size_t initial_chunk_size; 60 | size_t chunk_size; 61 | size_t last_chunk_size; 62 | size_t initial_requested_size; 63 | 64 | ngx_chain_t *free; // list of free buffers 65 | ngx_chain_t *busy; // list of busy buffers (being sent to client) 66 | ngx_uint_t allocated_count; // number of allocated buffers 67 | 68 | ngx_int_t error_code; 69 | ngx_http_event_handler_pt original_write_event_handler; 70 | 71 | ngx_str_t etag; 72 | ngx_str_t last_modified; 73 | 74 | ngx_uint_t active_fibers; 75 | ngx_http_parallel_fiber_ctx_t* completed_fiber; 76 | ngx_http_parallel_fiber_ctx_t* free_fibers; // a linked list of free fibers 77 | ngx_http_parallel_fiber_ctx_t fibers[1]; // must be last 78 | } ngx_http_parallel_ctx_t; 79 | 80 | // forward declarations 81 | static ngx_int_t ngx_http_parallel_subrequest_finished_handler( 82 | ngx_http_request_t *r, void *data, ngx_int_t rc); 83 | static char *ngx_http_parallel_command( 84 | ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 85 | static char *ngx_http_parallel_cache_command( 86 | ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 87 | static ngx_int_t ngx_http_parallel_filter_init(ngx_conf_t *cf); 88 | static void *ngx_http_parallel_create_loc_conf(ngx_conf_t *cf); 89 | static char *ngx_http_parallel_merge_loc_conf( 90 | ngx_conf_t *cf, void *parent, void *child); 91 | 92 | // constants 93 | static ngx_conf_num_bounds_t ngx_http_parallel_fiber_count_bounds = { 94 | ngx_conf_check_num_bounds, 2, 64 95 | }; 96 | 97 | static ngx_command_t ngx_http_parallel_commands[] = { 98 | 99 | { ngx_string("parallel"), 100 | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 101 | ngx_http_parallel_command, 102 | NGX_HTTP_LOC_CONF_OFFSET, 103 | 0, 104 | NULL}, 105 | 106 | { ngx_string("parallel_fiber_count"), 107 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 108 | ngx_conf_set_num_slot, 109 | NGX_HTTP_LOC_CONF_OFFSET, 110 | offsetof(ngx_http_parallel_loc_conf_t, fiber_count), 111 | &ngx_http_parallel_fiber_count_bounds }, 112 | 113 | { ngx_string("parallel_min_chunk_size"), 114 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 115 | ngx_conf_set_size_slot, 116 | NGX_HTTP_LOC_CONF_OFFSET, 117 | offsetof(ngx_http_parallel_loc_conf_t, min_chunk_size), 118 | NULL }, 119 | 120 | { ngx_string("parallel_max_chunk_size"), 121 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 122 | ngx_conf_set_size_slot, 123 | NGX_HTTP_LOC_CONF_OFFSET, 124 | offsetof(ngx_http_parallel_loc_conf_t, max_chunk_size), 125 | NULL }, 126 | 127 | { ngx_string("parallel_max_headers_size"), 128 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 129 | ngx_conf_set_size_slot, 130 | NGX_HTTP_LOC_CONF_OFFSET, 131 | offsetof(ngx_http_parallel_loc_conf_t, max_headers_size), 132 | NULL }, 133 | 134 | { ngx_string("parallel_max_buffer_count"), 135 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 136 | ngx_conf_set_num_slot, 137 | NGX_HTTP_LOC_CONF_OFFSET, 138 | offsetof(ngx_http_parallel_loc_conf_t, max_buffer_count), 139 | NULL }, 140 | 141 | { ngx_string("parallel_consistency_check_etag"), 142 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 143 | ngx_conf_set_flag_slot, 144 | NGX_HTTP_LOC_CONF_OFFSET, 145 | offsetof(ngx_http_parallel_loc_conf_t, consistency_check_etag), 146 | NULL }, 147 | 148 | { ngx_string("parallel_consistency_check_last_modified"), 149 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 150 | ngx_conf_set_flag_slot, 151 | NGX_HTTP_LOC_CONF_OFFSET, 152 | offsetof(ngx_http_parallel_loc_conf_t, consistency_check_last_modified), 153 | NULL }, 154 | 155 | { ngx_string("parallel_content_length_cache"), 156 | NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE12, 157 | ngx_http_parallel_cache_command, 158 | NGX_HTTP_LOC_CONF_OFFSET, 159 | offsetof(ngx_http_parallel_loc_conf_t, content_length_cache_zone), 160 | NULL }, 161 | 162 | ngx_null_command 163 | }; 164 | 165 | static ngx_http_module_t ngx_http_parallel_module_ctx = { 166 | NULL, /* preconfiguration */ 167 | ngx_http_parallel_filter_init, /* postconfiguration */ 168 | 169 | NULL, /* create main configuration */ 170 | NULL, /* init main configuration */ 171 | 172 | NULL, /* create server configuration */ 173 | NULL, /* merge server configuration */ 174 | 175 | ngx_http_parallel_create_loc_conf, /* create location configuration */ 176 | ngx_http_parallel_merge_loc_conf /* merge location configuration */ 177 | }; 178 | 179 | ngx_module_t ngx_http_parallel_module = { 180 | NGX_MODULE_V1, 181 | &ngx_http_parallel_module_ctx, /* module context */ 182 | ngx_http_parallel_commands, /* module directives */ 183 | NGX_HTTP_MODULE, /* module type */ 184 | NULL, /* init master */ 185 | NULL, /* init module */ 186 | NULL, /* init process */ 187 | NULL, /* init thread */ 188 | NULL, /* exit thread */ 189 | NULL, /* exit process */ 190 | NULL, /* exit master */ 191 | NGX_MODULE_V1_PADDING 192 | }; 193 | 194 | static ngx_uint_t content_range_hash = 195 | ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash( 196 | ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash( 197 | 'c', 'o'), 'n'), 't'), 'e'), 'n'), 't'), '-'), 'r'), 'a'), 'n'), 'g'), 'e'); 198 | static ngx_str_t content_range_name = ngx_string("content-range"); 199 | 200 | // globals 201 | static ngx_http_output_body_filter_pt ngx_http_next_body_filter; 202 | 203 | // Note: the code below is a slightly modified version of ngx_http_range_parse, 204 | // it has a few limitations compared to the code found there: 205 | // 1. only a single range is supported 206 | // 2. suffix ranges (e.g -100) are not supported 207 | static ngx_int_t 208 | ngx_http_parallel_range_parse(ngx_http_request_t *r, ngx_http_range_t* result) 209 | { 210 | u_char *p; 211 | off_t start, end, cutoff, cutlim; 212 | 213 | if (r->headers_in.range->value.len < 7 || 214 | ngx_strncasecmp(r->headers_in.range->value.data, 215 | (u_char *) "bytes=", 6) != 0) { 216 | return NGX_HTTP_RANGE_NOT_SATISFIABLE; 217 | } 218 | 219 | p = r->headers_in.range->value.data + 6; 220 | 221 | cutoff = NGX_MAX_OFF_T_VALUE / 10; 222 | cutlim = NGX_MAX_OFF_T_VALUE % 10; 223 | 224 | start = 0; 225 | end = 0; 226 | 227 | while (*p == ' ') { p++; } 228 | 229 | if (*p < '0' || *p > '9') { 230 | return NGX_HTTP_RANGE_NOT_SATISFIABLE; 231 | } 232 | 233 | while (*p >= '0' && *p <= '9') { 234 | if (start >= cutoff && (start > cutoff || *p - '0' > cutlim)) { 235 | return NGX_HTTP_RANGE_NOT_SATISFIABLE; 236 | } 237 | 238 | start = start * 10 + *p++ - '0'; 239 | } 240 | 241 | while (*p == ' ') { p++; } 242 | 243 | if (*p++ != '-') { 244 | return NGX_HTTP_RANGE_NOT_SATISFIABLE; 245 | } 246 | 247 | while (*p == ' ') { p++; } 248 | 249 | if (*p == '\0') { 250 | end = 0; // 0 signifies no limit 251 | goto found; 252 | } 253 | 254 | if (*p < '0' || *p > '9') { 255 | return NGX_HTTP_RANGE_NOT_SATISFIABLE; 256 | } 257 | 258 | while (*p >= '0' && *p <= '9') { 259 | if (end >= cutoff && (end > cutoff || *p - '0' > cutlim)) { 260 | return NGX_HTTP_RANGE_NOT_SATISFIABLE; 261 | } 262 | 263 | end = end * 10 + *p++ - '0'; 264 | } 265 | 266 | while (*p == ' ') { p++; } 267 | 268 | if (*p != '\0') { 269 | return NGX_HTTP_RANGE_NOT_SATISFIABLE; 270 | } 271 | 272 | end++; 273 | 274 | found: 275 | 276 | if (end != 0 && start >= end) { 277 | return NGX_HTTP_RANGE_NOT_SATISFIABLE; 278 | } 279 | 280 | result->start = start; 281 | result->end = end; 282 | 283 | return NGX_OK; 284 | } 285 | 286 | static void * 287 | ngx_http_parallel_memrchr(const u_char *s, int c, size_t n) 288 | { 289 | const u_char *cp; 290 | 291 | for (cp = s + n; cp > s;) 292 | { 293 | if (*(--cp) == (u_char)c) 294 | { 295 | return (void*)cp; 296 | } 297 | } 298 | return NULL; 299 | } 300 | 301 | static ngx_uint_t 302 | ngx_http_parallel_list_get_count(ngx_list_t* list) 303 | { 304 | ngx_list_part_t *part; 305 | ngx_uint_t result; 306 | 307 | result = 0; 308 | for (part = &list->part; part; part = part->next) 309 | { 310 | result += part->nelts; 311 | } 312 | return result; 313 | } 314 | 315 | static ngx_int_t 316 | ngx_http_parallel_list_copy( 317 | ngx_http_request_t* r, 318 | ngx_list_t* dest, 319 | ngx_list_t* src, 320 | ngx_uint_t n, 321 | size_t size) 322 | { 323 | ngx_list_part_t *part; 324 | ngx_int_t rc; 325 | u_char* p; 326 | 327 | rc = ngx_list_init(dest, r->pool, n, size); 328 | if (rc != NGX_OK) 329 | { 330 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 331 | "ngx_http_parallel_list_copy: ngx_list_init failed"); 332 | return NGX_ERROR; 333 | } 334 | 335 | p = (u_char*)dest->last->elts; 336 | 337 | for (part = &r->headers_in.headers.part; part; part = part->next) 338 | { 339 | p = ngx_copy(p, part->elts, size * part->nelts); 340 | dest->last->nelts += part->nelts; 341 | } 342 | 343 | return NGX_OK; 344 | } 345 | 346 | // the following function updates the pointers in headers_in (e.g. range) 347 | // to point to the respective elements inside the headers_in->headers array 348 | static void 349 | ngx_http_parallel_update_headers( 350 | ngx_http_request_t* r, 351 | ngx_http_headers_in_t* headers_in) 352 | { 353 | ngx_http_core_main_conf_t *cmcf; 354 | ngx_http_header_t *hh; 355 | ngx_list_part_t *part; 356 | ngx_table_elt_t **ph; 357 | ngx_table_elt_t *h; 358 | ngx_uint_t i; 359 | 360 | cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); 361 | 362 | part = &headers_in->headers.part; 363 | h = part->elts; 364 | 365 | for (i = 0; /* void */; i++) 366 | { 367 | if (i >= part->nelts) 368 | { 369 | if (part->next == NULL) 370 | { 371 | break; 372 | } 373 | 374 | part = part->next; 375 | h = part->elts; 376 | i = 0; 377 | } 378 | 379 | hh = ngx_hash_find(&cmcf->headers_in_hash, h[i].hash, 380 | h[i].lowcase_key, h[i].key.len); 381 | if (!hh) 382 | { 383 | continue; 384 | } 385 | 386 | ph = (ngx_table_elt_t **)((char *)headers_in + hh->offset); 387 | 388 | *ph = &h[i]; 389 | } 390 | } 391 | 392 | static ngx_str_t* 393 | ngx_http_parallel_header_get_value( 394 | ngx_list_t* headers, 395 | ngx_str_t* name, 396 | ngx_uint_t hash) 397 | { 398 | ngx_list_part_t *part; 399 | ngx_table_elt_t *h; 400 | ngx_uint_t i; 401 | 402 | part = &headers->part; 403 | h = part->elts; 404 | 405 | for (i = 0; /* void */; i++) 406 | { 407 | if (i >= part->nelts) 408 | { 409 | if (part->next == NULL) 410 | { 411 | break; 412 | } 413 | 414 | part = part->next; 415 | h = part->elts; 416 | i = 0; 417 | } 418 | 419 | if (h[i].hash == hash && 420 | h[i].key.len == name->len && 421 | ngx_memcmp(h[i].lowcase_key, name->data, name->len) == 0) 422 | { 423 | return &h[i].value; 424 | } 425 | } 426 | 427 | return NULL; 428 | } 429 | 430 | static void 431 | ngx_http_parallel_header_clear_value( 432 | ngx_list_t* headers, 433 | ngx_str_t* name, 434 | ngx_uint_t hash) 435 | { 436 | ngx_list_part_t *part; 437 | ngx_table_elt_t *h; 438 | ngx_uint_t i = 0; 439 | 440 | part = &headers->part; 441 | h = part->elts; 442 | 443 | for (;;) 444 | { 445 | if (i >= part->nelts) 446 | { 447 | if (part->next == NULL) 448 | { 449 | break; 450 | } 451 | 452 | part = part->next; 453 | h = part->elts; 454 | i = 0; 455 | } 456 | 457 | if (h[i].hash != hash || 458 | h[i].key.len != name->len || 459 | ngx_memcmp(h[i].lowcase_key, name->data, name->len) != 0) 460 | { 461 | i++; 462 | continue; 463 | } 464 | 465 | ngx_memmove(h + i, h + i + 1, (part->nelts - i - 1) * sizeof(*h)); 466 | part->nelts--; 467 | } 468 | } 469 | 470 | static off_t 471 | ngx_http_parallel_get_instance_length(ngx_str_t *content_range, ngx_log_t* log) 472 | { 473 | u_char* last_slash; 474 | off_t content_length; 475 | 476 | last_slash = ngx_http_parallel_memrchr( 477 | content_range->data, '/', content_range->len); 478 | if (last_slash == NULL) 479 | { 480 | ngx_log_error(NGX_LOG_ERR, log, 0, 481 | "ngx_http_parallel_get_instance_length: " 482 | "no slash in content-range value \"%V\"", content_range); 483 | return -1; 484 | } 485 | 486 | content_length = ngx_atoof( 487 | last_slash + 1, 488 | content_range->data + content_range->len - last_slash - 1); 489 | if (content_length < 0) 490 | { 491 | ngx_log_error(NGX_LOG_ERR, log, 0, 492 | "ngx_http_parallel_get_instance_length: failed to parse " 493 | "instance length from content-range \"%V\"", content_range); 494 | return -1; 495 | } 496 | 497 | return content_length; 498 | } 499 | 500 | static ngx_int_t 501 | ngx_http_parallel_init_fiber( 502 | ngx_http_request_t *r, 503 | ngx_uint_t header_in_count, 504 | ngx_flag_t proxy_as_is, 505 | ngx_http_parallel_fiber_ctx_t* fiber) 506 | { 507 | ngx_table_elt_t *h; 508 | ngx_int_t rc; 509 | 510 | // create the post-subrequest 511 | fiber->psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t)); 512 | if (fiber->psr == NULL) 513 | { 514 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 515 | "ngx_http_parallel_init_fiber: ngx_palloc failed"); 516 | return NGX_ERROR; 517 | } 518 | 519 | fiber->psr->handler = ngx_http_parallel_subrequest_finished_handler; 520 | fiber->psr->data = fiber; 521 | 522 | // copy the input headers 523 | fiber->headers_in = r->headers_in; 524 | 525 | if (proxy_as_is) 526 | { 527 | return NGX_OK; 528 | } 529 | 530 | // copy the headers list in order to update it 531 | rc = ngx_http_parallel_list_copy( 532 | r, 533 | &fiber->headers_in.headers, 534 | &r->headers_in.headers, 535 | header_in_count + 1, 536 | sizeof(ngx_table_elt_t)); 537 | if (rc != NGX_OK) 538 | { 539 | return rc; 540 | } 541 | 542 | ngx_http_parallel_update_headers(r, &fiber->headers_in); 543 | 544 | // add a range header 545 | if (fiber->headers_in.range == NULL) 546 | { 547 | h = ngx_list_push(&fiber->headers_in.headers); 548 | if (h == NULL) 549 | { 550 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 551 | "ngx_http_parallel_init_fiber: ngx_list_push failed"); 552 | return NGX_ERROR; 553 | } 554 | 555 | h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash('r', 'a'), 'n'), 'g'), 'e'); 556 | 557 | h->key.data = (u_char*)"Range"; 558 | h->key.len = sizeof("Range") - 1; 559 | 560 | h->lowcase_key = (u_char*)"range"; 561 | 562 | fiber->headers_in.range = h; 563 | } 564 | else 565 | { 566 | h = fiber->headers_in.range; 567 | } 568 | 569 | h->value.data = ngx_pnalloc(r->pool, sizeof(RANGE_FORMAT) + 2 * NGX_OFF_T_LEN); 570 | if (h->value.data == NULL) 571 | { 572 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 573 | "ngx_http_parallel_init_fiber: ngx_pnalloc failed"); 574 | return NGX_ERROR; 575 | } 576 | 577 | return NGX_OK; 578 | } 579 | 580 | // this function is required in order to support buffer reuse - it hooks the 581 | // initial call to ngx_http_handler. after this function returns the upstream 582 | // is already allocated by the proxy module, so it's possible to set its buffer 583 | static void 584 | ngx_http_parallel_fiber_initial_wev_handler(ngx_http_request_t *r) 585 | { 586 | ngx_http_parallel_fiber_ctx_t* fiber; 587 | ngx_http_upstream_t* u; 588 | ngx_connection_t *c; 589 | 590 | c = r->connection; 591 | 592 | // call the default request handler 593 | r->write_event_handler = ngx_http_handler; 594 | ngx_http_handler(r); 595 | 596 | // if request was destoryed ignore 597 | if (c->destroyed) 598 | { 599 | return; 600 | } 601 | 602 | // at this point the upstream should have been allocated by the proxy module 603 | u = r->upstream; 604 | if (u == NULL) 605 | { 606 | return; 607 | } 608 | 609 | // initialize the upstream buffer 610 | fiber = ngx_http_get_module_ctx(r, ngx_http_parallel_module); 611 | u->buffer = *fiber->cl->buf; 612 | 613 | // initialize the headers list 614 | u->headers_in.headers = fiber->upstream_headers; 615 | u->headers_in.headers.last = &u->headers_in.headers.part; 616 | } 617 | 618 | static ngx_int_t 619 | ngx_http_parallel_start_fiber( 620 | ngx_http_request_t *r, 621 | ngx_http_parallel_fiber_ctx_t* fiber, 622 | uint64_t chunk_index) 623 | { 624 | ngx_http_parallel_loc_conf_t *conf; 625 | ngx_http_parallel_ctx_t *ctx; 626 | ngx_http_request_t* sr; 627 | ngx_str_t args = ngx_null_string; 628 | ngx_table_elt_t *h; 629 | ngx_int_t rc; 630 | size_t alloc_size; 631 | off_t start_offset; 632 | off_t end_offset; 633 | ngx_chain_t* cl; 634 | ngx_buf_t* b; 635 | 636 | ctx = ngx_http_get_module_ctx(r, ngx_http_parallel_module); 637 | conf = ngx_http_get_module_loc_conf(r, ngx_http_parallel_module); 638 | 639 | // get a buffer for the response 640 | cl = ngx_chain_get_free_buf(r->pool, &ctx->free); 641 | if (cl == NULL) 642 | { 643 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 644 | "ngx_http_parallel_start_fiber: ngx_chain_get_free_buf failed"); 645 | return NGX_ERROR; 646 | } 647 | 648 | b = cl->buf; 649 | 650 | if (cl->buf->start == NULL) 651 | { 652 | alloc_size = conf->max_headers_size + conf->max_chunk_size; 653 | b->start = ngx_palloc(r->pool, alloc_size); 654 | if (b->start == NULL) 655 | { 656 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 657 | "ngx_http_parallel_start_fiber: ngx_palloc failed"); 658 | return NGX_ERROR; 659 | } 660 | 661 | b->pos = b->start; 662 | b->last = b->start; 663 | b->end = b->last + alloc_size; 664 | b->temporary = 1; 665 | b->tag = (ngx_buf_tag_t)&ngx_http_parallel_module; 666 | 667 | ctx->allocated_count++; 668 | } 669 | 670 | // allocate a list for the input headers 671 | if (ngx_list_init(&fiber->upstream_headers, r->pool, 8, 672 | sizeof(ngx_table_elt_t)) != NGX_OK) 673 | { 674 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 675 | "ngx_http_parallel_start_fiber: ngx_list_init failed"); 676 | return NGX_ERROR; 677 | } 678 | 679 | // set the range 680 | if (ctx->fiber_count != 1) 681 | { 682 | start_offset = ctx->range.start; 683 | if (chunk_index < ctx->fiber_count) 684 | { 685 | start_offset += chunk_index * ctx->chunk_size; 686 | } 687 | else 688 | { 689 | start_offset += ctx->initial_requested_size + 690 | (chunk_index - ctx->fiber_count) * ctx->chunk_size; 691 | } 692 | 693 | end_offset = start_offset + ctx->chunk_size; 694 | if (ctx->range.end != 0 && ctx->range.end < end_offset) 695 | { 696 | end_offset = ctx->range.end; 697 | } 698 | 699 | h = fiber->headers_in.range; 700 | h->value.len = ngx_sprintf( 701 | h->value.data, 702 | RANGE_FORMAT, 703 | start_offset, 704 | end_offset - 1) - h->value.data; 705 | h->value.data[h->value.len] = '\0'; 706 | } 707 | 708 | // start the request 709 | r->headers_in = fiber->headers_in; 710 | rc = ngx_http_subrequest(r, &ctx->sr_uri, &args, &sr, fiber->psr, 711 | NGX_HTTP_SUBREQUEST_WAITED | NGX_HTTP_SUBREQUEST_IN_MEMORY); 712 | r->headers_in = ctx->original_headers_in; 713 | 714 | if (rc != NGX_OK) 715 | { 716 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 717 | "ngx_http_parallel_start_fiber: ngx_http_subrequest failed %i", rc); 718 | return rc; 719 | } 720 | 721 | ngx_http_set_ctx(sr, fiber, ngx_http_parallel_module); 722 | 723 | ctx->active_fibers++; 724 | 725 | sr->write_event_handler = ngx_http_parallel_fiber_initial_wev_handler; 726 | sr->method = r->method; // copy the method to the subrequest 727 | sr->method_name = r->method_name; // (ngx_http_subrequest always uses GET) 728 | sr->header_in = r->header_in; 729 | 730 | // fix the last pointer in headers_in (from echo-nginx-module) 731 | if (fiber->headers_in.headers.last == &fiber->headers_in.headers.part) { 732 | sr->headers_in.headers.last = &sr->headers_in.headers.part; 733 | } 734 | 735 | fiber->chunk_index = chunk_index; 736 | fiber->cl = cl; 737 | fiber->sr = sr; 738 | 739 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 740 | "ngx_http_parallel_start_fiber: started fiber %uL", chunk_index); 741 | 742 | return NGX_OK; 743 | } 744 | 745 | static ngx_int_t 746 | ngx_http_parallel_start_fibers( 747 | ngx_http_parallel_ctx_t *ctx, 748 | ngx_http_request_t *r, 749 | ngx_http_parallel_loc_conf_t *conf) 750 | { 751 | ngx_http_parallel_fiber_ctx_t* fiber; 752 | ngx_int_t rc; 753 | 754 | while (ctx->free_fibers && 755 | (ctx->free || ctx->allocated_count < conf->max_buffer_count) && 756 | ctx->next_request_chunk < ctx->chunk_count) 757 | { 758 | // remove from free list 759 | fiber = ctx->free_fibers; 760 | ctx->free_fibers = fiber->next; 761 | 762 | // start the fiber 763 | rc = ngx_http_parallel_start_fiber(r, fiber, ctx->next_request_chunk); 764 | if (rc != NGX_OK) 765 | { 766 | return rc; 767 | } 768 | 769 | ctx->next_request_chunk++; 770 | } 771 | 772 | return NGX_OK; 773 | } 774 | 775 | static void 776 | ngx_http_parallel_calculate_key( 777 | u_char* key, 778 | ngx_http_request_t *r) 779 | { 780 | ngx_md5_t md5; 781 | 782 | ngx_md5_init(&md5); 783 | if (r->headers_in.host != NULL) 784 | { 785 | ngx_md5_update(&md5, r->headers_in.host->value.data, r->headers_in.host->value.len); 786 | } 787 | ngx_md5_update(&md5, r->uri.data, r->uri.len); 788 | ngx_md5_final(key, &md5); 789 | } 790 | 791 | static ngx_int_t 792 | ngx_http_parallel_init_chunks( 793 | ngx_http_parallel_ctx_t *ctx, 794 | ngx_http_request_t *r, 795 | ngx_http_headers_out_t* headers_out) 796 | { 797 | ngx_http_parallel_loc_conf_t *conf; 798 | ngx_table_elt_t *h; 799 | ngx_str_t* content_range; 800 | uint64_t missing_chunks_mask; 801 | off_t instance_length; 802 | off_t remaining_length; 803 | off_t content_length; 804 | ngx_int_t rc; 805 | 806 | // find the instance length 807 | content_range = ngx_http_parallel_header_get_value( 808 | &headers_out->headers, 809 | &content_range_name, 810 | content_range_hash); 811 | if (content_range == NULL) 812 | { 813 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 814 | "ngx_http_parallel_init_chunks: failed to get content-range header"); 815 | return NGX_HTTP_BAD_GATEWAY; 816 | } 817 | 818 | instance_length = ngx_http_parallel_get_instance_length( 819 | content_range, 820 | r->connection->log); 821 | if (instance_length < 0) 822 | { 823 | return NGX_HTTP_BAD_GATEWAY; 824 | } 825 | 826 | conf = ngx_http_get_module_loc_conf(r, ngx_http_parallel_module); 827 | 828 | // update cache 829 | if (conf->content_length_cache_zone != NULL && 830 | instance_length != ctx->cached_response_length) 831 | { 832 | if (!ctx->key_inited) 833 | { 834 | ngx_http_parallel_calculate_key(ctx->key, r); 835 | } 836 | 837 | ngx_fixed_buffer_cache_store( 838 | conf->content_length_cache_zone, 839 | ctx->key, 840 | (u_char*)&instance_length, 841 | 1); 842 | } 843 | 844 | // find the content length 845 | content_length = instance_length; 846 | 847 | if (ctx->range.end != 0 && ctx->range.end < content_length) 848 | { 849 | content_length = ctx->range.end; 850 | } 851 | 852 | if (content_length < ctx->range.start) 853 | { 854 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 855 | "ngx_http_parallel_init_chunks: " 856 | "unexpected, content length %O less than range start %O", 857 | content_length, ctx->range.start); 858 | return NGX_HTTP_BAD_GATEWAY; 859 | } 860 | 861 | content_length -= ctx->range.start; 862 | 863 | // find the chunk size and count 864 | if (content_length <= (off_t)ctx->initial_requested_size) 865 | { 866 | ctx->chunk_count = DIV_CEIL(content_length, ctx->chunk_size); 867 | ctx->last_chunk_size = content_length + ctx->chunk_size - 868 | ctx->chunk_count * ctx->chunk_size; 869 | } 870 | else 871 | { 872 | remaining_length = content_length - ctx->initial_requested_size; 873 | if (remaining_length <= (off_t)(ctx->fiber_count * conf->min_chunk_size)) 874 | { 875 | ctx->chunk_size = conf->min_chunk_size; 876 | } 877 | else if (remaining_length >= (off_t)(ctx->fiber_count * conf->max_chunk_size)) 878 | { 879 | ctx->chunk_size = conf->max_chunk_size; 880 | } 881 | else 882 | { 883 | ctx->chunk_size = DIV_CEIL(remaining_length, ctx->fiber_count); 884 | } 885 | ctx->chunk_count = DIV_CEIL(remaining_length, ctx->chunk_size); 886 | ctx->last_chunk_size = remaining_length + ctx->chunk_size - 887 | ctx->chunk_count * ctx->chunk_size; 888 | ctx->chunk_count += ctx->fiber_count; 889 | } 890 | 891 | ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 892 | "ngx_http_parallel_init_chunks: " 893 | "chunk count %uL chunk size %uz last chunk size %uz", 894 | ctx->chunk_count, ctx->chunk_size, ctx->last_chunk_size); 895 | 896 | // check for missing chunks 897 | missing_chunks_mask = ULLONG_MAX; 898 | if (ctx->chunk_count < 64) 899 | { 900 | missing_chunks_mask = ((1ULL << ctx->chunk_count) - 1); 901 | } 902 | 903 | if ((ctx->missing_chunks & missing_chunks_mask) != 0) 904 | { 905 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 906 | "ngx_http_parallel_init_chunks: " 907 | "missing chunks 0x%uxL chunk count %uL", 908 | ctx->missing_chunks, ctx->chunk_count); 909 | return NGX_HTTP_BAD_GATEWAY; 910 | } 911 | 912 | // initialize the chunks array (null terminated) 913 | ctx->chunks = ngx_pcalloc(r->pool, sizeof(ctx->chunks[0]) * (ctx->chunk_count + 1)); 914 | if (ctx->chunks == NULL) 915 | { 916 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 917 | "ngx_http_parallel_init_chunks: ngx_pcalloc failed"); 918 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 919 | } 920 | 921 | // save headers for consistency check 922 | if (conf->consistency_check_etag) 923 | { 924 | h = headers_out->etag; 925 | if (h != NULL) 926 | { 927 | ctx->etag.data = ngx_pstrdup(r->pool, &h->value); 928 | if (ctx->etag.data == NULL) 929 | { 930 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 931 | "ngx_http_parallel_init_chunks: ngx_pstrdup failed (1)"); 932 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 933 | } 934 | 935 | ctx->etag.len = h->value.len; 936 | } 937 | } 938 | 939 | if (conf->consistency_check_last_modified) 940 | { 941 | h = headers_out->last_modified; 942 | if (h != NULL) 943 | { 944 | ctx->last_modified.data = ngx_pstrdup(r->pool, &h->value); 945 | if (ctx->last_modified.data == NULL) 946 | { 947 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 948 | "ngx_http_parallel_init_chunks: ngx_pstrdup failed (2)"); 949 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 950 | } 951 | 952 | ctx->last_modified.len = h->value.len; 953 | } 954 | } 955 | 956 | // build the response headers 957 | r->headers_out = *headers_out; 958 | 959 | if (ctx->range.is_range_request) 960 | { 961 | // leave the status as 206 and update the content range 962 | content_range->data = ngx_pnalloc( 963 | r->pool, sizeof(CONTENT_RANGE_FORMAT) + 3 * NGX_OFF_T_LEN); 964 | if (content_range->data == NULL) 965 | { 966 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 967 | "ngx_http_parallel_init_chunks: ngx_pnalloc failed"); 968 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 969 | } 970 | 971 | content_range->len = ngx_sprintf(content_range->data, 972 | CONTENT_RANGE_FORMAT, 973 | ctx->range.start, 974 | ctx->range.start + content_length - 1, 975 | instance_length) - content_range->data; 976 | } 977 | else 978 | { 979 | // change status to 200 and clear the content range 980 | r->headers_out.status = NGX_HTTP_OK; 981 | r->headers_out.status_line.len = 0; 982 | 983 | ngx_http_parallel_header_clear_value( 984 | &r->headers_out.headers, &content_range_name, content_range_hash); 985 | } 986 | 987 | ngx_http_clear_content_length(r); 988 | r->headers_out.content_length_n = content_length; 989 | 990 | // send the response headers 991 | rc = ngx_http_send_header(r); 992 | if (rc == NGX_ERROR || rc > NGX_OK) 993 | { 994 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 995 | "ngx_http_parallel_init_chunks: ngx_http_send_header failed %i", rc); 996 | return rc; 997 | } 998 | 999 | return NGX_OK; 1000 | } 1001 | 1002 | static ngx_flag_t 1003 | ngx_http_parallel_check_consistency( 1004 | ngx_http_parallel_ctx_t *ctx, 1005 | ngx_http_request_t *r, 1006 | ngx_http_parallel_loc_conf_t *conf) 1007 | { 1008 | ngx_table_elt_t* h; 1009 | 1010 | if (conf->consistency_check_etag) 1011 | { 1012 | h = r->headers_out.etag; 1013 | if (h != NULL) 1014 | { 1015 | if (ctx->etag.len != h->value.len || 1016 | ngx_memcmp(ctx->etag.data, h->value.data, ctx->etag.len) != 0) 1017 | { 1018 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1019 | "ngx_http_parallel_check_consistency: " 1020 | "inconsistent etag, original=\"%V\" current=\"%V\"", 1021 | &ctx->etag, &h->value); 1022 | return 0; 1023 | } 1024 | } 1025 | else 1026 | { 1027 | if (ctx->etag.len != 0) 1028 | { 1029 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1030 | "ngx_http_parallel_check_consistency: " 1031 | "inconsistent etag, original=\"%V\" current=\"\"", 1032 | &ctx->etag); 1033 | return 0; 1034 | } 1035 | } 1036 | } 1037 | 1038 | if (conf->consistency_check_last_modified) 1039 | { 1040 | h = r->headers_out.last_modified; 1041 | if (h != NULL) 1042 | { 1043 | if (ctx->last_modified.len != h->value.len || 1044 | ngx_memcmp(ctx->last_modified.data, h->value.data, 1045 | ctx->last_modified.len) != 0) 1046 | { 1047 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1048 | "ngx_http_parallel_check_consistency: " 1049 | "inconsistent last-modified, original=\"%V\" current=\"%V\"", 1050 | &ctx->last_modified, &h->value); 1051 | return 0; 1052 | } 1053 | } 1054 | else 1055 | { 1056 | if (ctx->last_modified.len != 0) 1057 | { 1058 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1059 | "ngx_http_parallel_check_consistency: " 1060 | "inconsistent last-modified, original=\"%V\" current=\"\"", 1061 | &ctx->last_modified); 1062 | return 0; 1063 | } 1064 | } 1065 | } 1066 | 1067 | return 1; 1068 | } 1069 | 1070 | static ngx_int_t 1071 | ngx_http_parallel_send_buffer( 1072 | ngx_http_parallel_ctx_t *ctx, 1073 | ngx_http_request_t *r, 1074 | ngx_chain_t* out) 1075 | { 1076 | ngx_chain_t* cl; 1077 | ngx_int_t rc; 1078 | 1079 | // add current buffer to chain 1080 | cl = out; 1081 | ctx->next_send_chunk++; 1082 | 1083 | // add any previously fetched contiguous buffers to the chain 1084 | for (; ctx->chunks[ctx->next_send_chunk]; ctx->next_send_chunk++) 1085 | { 1086 | cl->next = ctx->chunks[ctx->next_send_chunk]; 1087 | cl = cl->next; 1088 | } 1089 | 1090 | // set the last buf flag if needed 1091 | if (ctx->next_send_chunk >= ctx->chunk_count) 1092 | { 1093 | cl->buf->last_buf = 1; 1094 | ctx->error_code = NGX_OK; 1095 | } 1096 | 1097 | // send the chain 1098 | rc = ngx_http_output_filter(r, out); 1099 | if (rc != NGX_OK && rc != NGX_AGAIN) 1100 | { 1101 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1102 | "ngx_http_parallel_send_buffer: " 1103 | "ngx_http_output_filter failed %i", rc); 1104 | return rc; 1105 | } 1106 | 1107 | // update the free / busy chains 1108 | ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &out, 1109 | (ngx_buf_tag_t)&ngx_http_parallel_module); 1110 | 1111 | return NGX_OK; 1112 | } 1113 | 1114 | static ngx_int_t 1115 | ngx_http_parallel_body_filter(ngx_http_request_t *r, ngx_chain_t *in) 1116 | { 1117 | ngx_http_parallel_loc_conf_t *conf; 1118 | ngx_http_parallel_ctx_t *ctx; 1119 | ngx_http_request_t *pr; 1120 | ngx_chain_t* out; 1121 | ngx_int_t rc; 1122 | 1123 | // ignore any requests that were not handled by this module 1124 | ctx = ngx_http_get_module_ctx(r, ngx_http_parallel_module); 1125 | if (ctx == NULL) 1126 | { 1127 | return ngx_http_next_body_filter(r, in); 1128 | } 1129 | 1130 | // ignore subrequests of requests handled by this module 1131 | pr = r->parent; 1132 | if (pr && ngx_http_get_module_ctx(pr, ngx_http_parallel_module)) 1133 | { 1134 | return ngx_http_next_body_filter(r, in); 1135 | } 1136 | 1137 | rc = ngx_http_next_body_filter(r, in); 1138 | 1139 | // check whether a buffer was finished 1140 | if (ctx->busy && ngx_buf_size(ctx->busy->buf) == 0 && 1141 | ctx->free_fibers && 1142 | ctx->next_request_chunk < ctx->chunk_count) 1143 | { 1144 | // move the buffer to the free chain 1145 | out = NULL; 1146 | ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &out, 1147 | (ngx_buf_tag_t)&ngx_http_parallel_module); 1148 | 1149 | // start fibers if possible 1150 | conf = ngx_http_get_module_loc_conf(r, ngx_http_parallel_module); 1151 | rc = ngx_http_parallel_start_fibers(ctx, r, conf); 1152 | if (rc != NGX_OK) 1153 | { 1154 | return NGX_ERROR; 1155 | } 1156 | } 1157 | 1158 | return rc; 1159 | } 1160 | 1161 | static ngx_int_t 1162 | ngx_http_parallel_handle_request_complete( 1163 | ngx_http_parallel_ctx_t *ctx, 1164 | ngx_http_request_t *r, 1165 | ngx_http_parallel_fiber_ctx_t* fiber) 1166 | { 1167 | ngx_http_parallel_loc_conf_t *conf; 1168 | ngx_http_upstream_t* u; 1169 | ngx_http_request_t* pr; 1170 | ngx_buf_t* b; 1171 | ngx_int_t rc; 1172 | off_t expected_size; 1173 | 1174 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1175 | "ngx_http_parallel_handle_request_complete: " 1176 | "fiber finished %uL", fiber->chunk_index); 1177 | 1178 | ctx->active_fibers--; 1179 | 1180 | // add the fiber to the free list 1181 | fiber->next = ctx->free_fibers; 1182 | ctx->free_fibers = fiber; 1183 | 1184 | if (ctx->error_code != NGX_AGAIN) 1185 | { 1186 | return NGX_OK; 1187 | } 1188 | 1189 | if (r->headers_out.status == NGX_HTTP_RANGE_NOT_SATISFIABLE && 1190 | fiber->chunk_index != 0) 1191 | { 1192 | if (ctx->chunks == NULL) 1193 | { 1194 | // don't have the response length yet, flag this chunk as missing 1195 | ctx->missing_chunks |= (1 << fiber->chunk_index); 1196 | } 1197 | else 1198 | { 1199 | if (fiber->chunk_index < ctx->chunk_count) 1200 | { 1201 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1202 | "ngx_http_parallel_handle_request_complete: " 1203 | "got 416 error for a required chunk %uL/%uL", 1204 | fiber->chunk_index, ctx->chunk_count); 1205 | return NGX_HTTP_BAD_GATEWAY; 1206 | } 1207 | } 1208 | return NGX_OK; 1209 | } 1210 | 1211 | pr = r->parent; 1212 | 1213 | if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT && pr->header_sent) 1214 | { 1215 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1216 | "ngx_http_parallel_handle_request_complete: " 1217 | "unexpected status %ui after getting 206"); 1218 | return NGX_HTTP_BAD_GATEWAY; 1219 | } 1220 | 1221 | // update the buffer pointers from the upstream buffer 1222 | u = r->upstream; 1223 | if (u == NULL) 1224 | { 1225 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1226 | "ngx_http_parallel_handle_request_complete: " 1227 | "unexpected, subrequest has no upstream"); 1228 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 1229 | } 1230 | 1231 | b = fiber->cl->buf; 1232 | *b = u->buffer; 1233 | b->last = b->pos + u->state->response_length; 1234 | 1235 | conf = ngx_http_get_module_loc_conf(pr, ngx_http_parallel_module); 1236 | 1237 | if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT || 1238 | ctx->fiber_count == 1) 1239 | { 1240 | // save the content length to cache 1241 | if (r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT && 1242 | ctx->cached_response_length != r->headers_out.content_length_n && 1243 | conf->content_length_cache_zone != NULL) 1244 | { 1245 | if (!ctx->key_inited) 1246 | { 1247 | ngx_http_parallel_calculate_key(ctx->key, r); 1248 | } 1249 | 1250 | ngx_fixed_buffer_cache_store( 1251 | conf->content_length_cache_zone, 1252 | ctx->key, 1253 | (u_char*)&r->headers_out.content_length_n, 1254 | 1); 1255 | } 1256 | 1257 | // copy the response headers from upstream 1258 | pr->headers_out = r->headers_out; 1259 | 1260 | rc = ngx_http_send_header(pr); 1261 | if (rc == NGX_ERROR || rc > NGX_OK) 1262 | { 1263 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1264 | "ngx_http_parallel_handle_request_complete: " 1265 | "ngx_http_send_header failed %i", rc); 1266 | return rc; 1267 | } 1268 | 1269 | // send the buffer 1270 | b->last_buf = 1; 1271 | ctx->error_code = NGX_OK; 1272 | 1273 | if (ngx_buf_size(b) == 0) 1274 | { 1275 | b->temporary = b->memory = 0; 1276 | } 1277 | 1278 | rc = ngx_http_output_filter(pr, fiber->cl); 1279 | if (rc != NGX_OK && rc != NGX_AGAIN) 1280 | { 1281 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1282 | "ngx_http_parallel_handle_request_complete: " 1283 | "ngx_http_output_filter failed %i", rc); 1284 | return rc; 1285 | } 1286 | 1287 | return NGX_OK; 1288 | } 1289 | 1290 | // initialize the chunks array (on first completed chunk) 1291 | if (ctx->chunks == NULL) 1292 | { 1293 | rc = ngx_http_parallel_init_chunks(ctx, pr, &r->headers_out); 1294 | if (rc != NGX_OK) 1295 | { 1296 | return rc; 1297 | } 1298 | } 1299 | else 1300 | { 1301 | if (!ngx_http_parallel_check_consistency(ctx, r, conf)) 1302 | { 1303 | return NGX_HTTP_BAD_GATEWAY; 1304 | } 1305 | } 1306 | 1307 | if (fiber->chunk_index >= ctx->chunk_count) 1308 | { 1309 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1310 | "ngx_http_parallel_handle_request_complete: " 1311 | "unexpected, chunk index %uL exceeds chunk count %uL", 1312 | fiber->chunk_index, ctx->chunk_count); 1313 | return NGX_HTTP_BAD_GATEWAY; 1314 | } 1315 | 1316 | // validate content length 1317 | if (fiber->chunk_index + 1 == ctx->chunk_count) 1318 | { 1319 | expected_size = ctx->last_chunk_size; 1320 | } 1321 | else if (fiber->chunk_index < ctx->fiber_count) 1322 | { 1323 | expected_size = ctx->initial_chunk_size; 1324 | } 1325 | else 1326 | { 1327 | expected_size = ctx->chunk_size; 1328 | } 1329 | 1330 | if (u->state->response_length != expected_size) 1331 | { 1332 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1333 | "ngx_http_parallel_handle_request_complete: " 1334 | "unexpected response length %O in chunk %uL/%uL, expected %O", 1335 | u->state->response_length, fiber->chunk_index, ctx->chunk_count, 1336 | expected_size); 1337 | return NGX_HTTP_BAD_GATEWAY; 1338 | } 1339 | 1340 | if (fiber->chunk_index == ctx->next_send_chunk) 1341 | { 1342 | // send the buffer and any queued buffers 1343 | rc = ngx_http_parallel_send_buffer(ctx, pr, fiber->cl); 1344 | if (rc != NGX_OK) 1345 | { 1346 | return rc; 1347 | } 1348 | } 1349 | else 1350 | { 1351 | // save the buffer to be sent later 1352 | ctx->chunks[fiber->chunk_index] = fiber->cl; 1353 | } 1354 | 1355 | // start fibers if possible 1356 | rc = ngx_http_parallel_start_fibers(ctx, pr, conf); 1357 | if (rc != NGX_OK) 1358 | { 1359 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 1360 | } 1361 | 1362 | return NGX_OK; 1363 | } 1364 | 1365 | static void 1366 | ngx_http_parallel_wev_handler(ngx_http_request_t *r) 1367 | { 1368 | ngx_http_parallel_fiber_ctx_t* fiber; 1369 | ngx_http_parallel_ctx_t *ctx; 1370 | ngx_int_t rc; 1371 | 1372 | ctx = ngx_http_get_module_ctx(r, ngx_http_parallel_module); 1373 | 1374 | // restore the write event handler 1375 | r->write_event_handler = ctx->original_write_event_handler; 1376 | ctx->original_write_event_handler = NULL; 1377 | 1378 | // get the fiber 1379 | fiber = ctx->completed_fiber; 1380 | ctx->completed_fiber = NULL; 1381 | 1382 | if (fiber == NULL) 1383 | { 1384 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1385 | "ngx_http_parallel_wev_handler: unexpected, fiber is null"); 1386 | return; 1387 | } 1388 | 1389 | // code taken from echo-nginx-module to work around nginx subrequest issues 1390 | if (r == r->connection->data && r->postponed) { 1391 | 1392 | if (r->postponed->request) { 1393 | r->connection->data = r->postponed->request; 1394 | 1395 | #if defined(nginx_version) && nginx_version >= 8012 1396 | ngx_http_post_request(r->postponed->request, NULL); 1397 | #else 1398 | ngx_http_post_request(r->postponed->request); 1399 | #endif 1400 | 1401 | } 1402 | else { 1403 | ngx_http_output_filter(r, NULL); 1404 | } 1405 | } 1406 | 1407 | rc = ngx_http_parallel_handle_request_complete(ctx, fiber->sr, fiber); 1408 | if (rc != NGX_OK && ctx->error_code == NGX_AGAIN) 1409 | { 1410 | ctx->error_code = rc; 1411 | } 1412 | 1413 | if (ctx->error_code != NGX_AGAIN && ctx->active_fibers == 0) 1414 | { 1415 | ngx_http_finalize_request(r, ctx->error_code); 1416 | } 1417 | } 1418 | 1419 | static ngx_int_t 1420 | ngx_http_parallel_subrequest_finished_handler( 1421 | ngx_http_request_t *r, 1422 | void *data, 1423 | ngx_int_t rc) 1424 | { 1425 | ngx_http_request_t *pr; 1426 | ngx_http_parallel_ctx_t *ctx; 1427 | 1428 | // make sure we are not called twice for the same request 1429 | r->post_subrequest = NULL; 1430 | 1431 | pr = r->parent; 1432 | 1433 | // save the completed fiber in the context for the write event handler 1434 | ctx = ngx_http_get_module_ctx(pr, ngx_http_parallel_module); 1435 | 1436 | ctx->completed_fiber = data; 1437 | if (rc != NGX_OK && ctx->error_code == NGX_AGAIN) 1438 | { 1439 | ctx->error_code = rc; 1440 | } 1441 | 1442 | if (ctx->original_write_event_handler != NULL) 1443 | { 1444 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1445 | "ngx_http_parallel_subrequest_finished_handler: " 1446 | "unexpected original_write_event_handler not null"); 1447 | return NGX_ERROR; 1448 | } 1449 | 1450 | ctx->original_write_event_handler = pr->write_event_handler; 1451 | pr->write_event_handler = ngx_http_parallel_wev_handler; 1452 | 1453 | /* work-around issues in nginx's event module */ 1454 | 1455 | if (r != r->connection->data 1456 | && r->postponed 1457 | && (r->main->posted_requests == NULL 1458 | || r->main->posted_requests->request != pr)) 1459 | { 1460 | #if defined(nginx_version) && nginx_version >= 8012 1461 | ngx_http_post_request(pr, NULL); 1462 | #else 1463 | ngx_http_post_request(pr); 1464 | #endif 1465 | } 1466 | 1467 | return NGX_OK; 1468 | } 1469 | 1470 | static ngx_int_t 1471 | ngx_http_parallel_handler(ngx_http_request_t *r) 1472 | { 1473 | ngx_http_parallel_loc_conf_t *conf; 1474 | ngx_http_parallel_ctx_t* ctx; 1475 | ngx_http_range_t range = { 0, 0, 0 }; 1476 | ngx_uint_t header_in_count; 1477 | ngx_uint_t fiber_count; 1478 | ngx_uint_t i; 1479 | ngx_flag_t key_inited = 0; 1480 | ngx_int_t rc; 1481 | u_char key[NGX_FIXED_BUFFER_CACHE_KEY_SIZE]; 1482 | size_t initial_chunk_size; 1483 | u_char* p; 1484 | off_t cached_response_length = -1; 1485 | off_t expected_response_length = -1; 1486 | 1487 | // validate method 1488 | if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) 1489 | { 1490 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 1491 | "ngx_http_parallel_handler: " 1492 | "unsupported method %ui", r->method); 1493 | return NGX_HTTP_NOT_ALLOWED; 1494 | } 1495 | 1496 | // discard request body, since we don't need it here 1497 | rc = ngx_http_discard_request_body(r); 1498 | if (rc != NGX_OK) 1499 | { 1500 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1501 | "ngx_http_parallel_handler: " 1502 | "ngx_http_discard_request_body failed %i", rc); 1503 | return rc; 1504 | } 1505 | 1506 | // get fiber count and initial chunk size 1507 | conf = ngx_http_get_module_loc_conf(r, ngx_http_parallel_module); 1508 | 1509 | fiber_count = conf->fiber_count; 1510 | initial_chunk_size = conf->min_chunk_size; 1511 | 1512 | if (r->method == NGX_HTTP_HEAD) 1513 | { 1514 | fiber_count = 1; 1515 | } 1516 | else if (r->headers_in.range != NULL) 1517 | { 1518 | rc = ngx_http_parallel_range_parse(r, &range); 1519 | if (rc != NGX_OK) 1520 | { 1521 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1522 | "ngx_http_parallel_handler: " 1523 | "ngx_http_parallel_range_parse failed \"%V\", " 1524 | "proxying the request as is", &r->headers_in.range->value); 1525 | fiber_count = 1; 1526 | } 1527 | else 1528 | { 1529 | range.is_range_request = 1; 1530 | 1531 | if (range.end != 0) 1532 | { 1533 | expected_response_length = range.end - range.start; 1534 | } 1535 | } 1536 | } 1537 | 1538 | if (expected_response_length < 0 && 1539 | fiber_count != 1 && 1540 | conf->content_length_cache_zone != NULL) 1541 | { 1542 | key_inited = 1; 1543 | 1544 | ngx_http_parallel_calculate_key(key, r); 1545 | 1546 | ngx_fixed_buffer_cache_fetch( 1547 | conf->content_length_cache_zone, 1548 | key, 1549 | (u_char*)&cached_response_length); 1550 | expected_response_length = cached_response_length; 1551 | } 1552 | 1553 | if (expected_response_length >= 0) 1554 | { 1555 | // optimize the initial chunk size according to the response length 1556 | if (expected_response_length <= (off_t)(fiber_count * conf->min_chunk_size)) 1557 | { 1558 | initial_chunk_size = conf->min_chunk_size; 1559 | } 1560 | else if (expected_response_length >= (off_t)(fiber_count * conf->max_chunk_size)) 1561 | { 1562 | initial_chunk_size = conf->max_chunk_size; 1563 | } 1564 | else 1565 | { 1566 | initial_chunk_size = DIV_CEIL(expected_response_length, fiber_count); 1567 | } 1568 | 1569 | // optimize the fiber count according to the response length 1570 | if (expected_response_length == 0) 1571 | { 1572 | fiber_count = 1; 1573 | } 1574 | else if (expected_response_length < (off_t)(fiber_count * initial_chunk_size)) 1575 | { 1576 | fiber_count = DIV_CEIL(expected_response_length, initial_chunk_size); 1577 | } 1578 | } 1579 | 1580 | // allocate context 1581 | ctx = ngx_pcalloc(r->pool, 1582 | sizeof(*ctx) + sizeof(ngx_http_parallel_fiber_ctx_t) * (fiber_count - 1)); 1583 | if (ctx == NULL) 1584 | { 1585 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1586 | "ngx_http_parallel_handler: ngx_pcalloc failed"); 1587 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 1588 | } 1589 | ctx->error_code = NGX_AGAIN; 1590 | ctx->initial_chunk_size = initial_chunk_size; 1591 | ctx->chunk_size = initial_chunk_size; 1592 | ctx->range = range; 1593 | ctx->fiber_count = fiber_count; 1594 | ctx->initial_requested_size = fiber_count * initial_chunk_size; 1595 | ctx->cached_response_length = cached_response_length; 1596 | if (key_inited) 1597 | { 1598 | ngx_memcpy(ctx->key, key, sizeof(ctx->key)); 1599 | ctx->key_inited = 1; 1600 | } 1601 | 1602 | ngx_http_set_ctx(r, ctx, ngx_http_parallel_module); 1603 | 1604 | // count the number of input headers 1605 | header_in_count = ngx_http_parallel_list_get_count(&r->headers_in.headers); 1606 | ctx->original_headers_in = r->headers_in; 1607 | 1608 | // build the subrequest uri 1609 | ctx->sr_uri.data = ngx_pnalloc(r->pool, conf->uri_prefix.len + r->uri.len + 1); 1610 | if (ctx->sr_uri.data == NULL) 1611 | { 1612 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1613 | "ngx_http_parallel_handler: ngx_pnalloc failed"); 1614 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 1615 | } 1616 | p = ngx_copy(ctx->sr_uri.data, conf->uri_prefix.data, conf->uri_prefix.len); 1617 | p = ngx_copy(p, r->uri.data, r->uri.len); 1618 | *p = '\0'; 1619 | ctx->sr_uri.len = p - ctx->sr_uri.data; 1620 | 1621 | // init and start the fibers 1622 | for (i = 0; i < fiber_count; i++) 1623 | { 1624 | rc = ngx_http_parallel_init_fiber( 1625 | r, header_in_count, fiber_count == 1, &ctx->fibers[i]); 1626 | if (rc != NGX_OK) 1627 | { 1628 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 1629 | } 1630 | 1631 | rc = ngx_http_parallel_start_fiber(r, &ctx->fibers[i], i); 1632 | if (rc != NGX_OK) 1633 | { 1634 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 1635 | } 1636 | } 1637 | 1638 | ctx->next_request_chunk = fiber_count; 1639 | 1640 | return NGX_AGAIN; 1641 | } 1642 | 1643 | static void * 1644 | ngx_http_parallel_create_loc_conf(ngx_conf_t *cf) 1645 | { 1646 | ngx_http_parallel_loc_conf_t *conf; 1647 | 1648 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_parallel_loc_conf_t)); 1649 | if (conf == NULL) 1650 | { 1651 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, 1652 | "ngx_http_parallel_create_loc_conf: ngx_pcalloc failed"); 1653 | return NGX_CONF_ERROR; 1654 | } 1655 | 1656 | conf->fiber_count = NGX_CONF_UNSET_UINT; 1657 | conf->min_chunk_size = NGX_CONF_UNSET_SIZE; 1658 | conf->max_chunk_size = NGX_CONF_UNSET_SIZE; 1659 | conf->max_headers_size = NGX_CONF_UNSET_SIZE; 1660 | conf->max_buffer_count = NGX_CONF_UNSET_UINT; 1661 | conf->consistency_check_etag = NGX_CONF_UNSET; 1662 | conf->consistency_check_last_modified = NGX_CONF_UNSET; 1663 | 1664 | return conf; 1665 | } 1666 | 1667 | static char * 1668 | ngx_http_parallel_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) 1669 | { 1670 | ngx_http_parallel_loc_conf_t *prev = parent; 1671 | ngx_http_parallel_loc_conf_t *conf = child; 1672 | 1673 | ngx_conf_merge_str_value(conf->uri_prefix, prev->uri_prefix, ""); 1674 | ngx_conf_merge_uint_value(conf->fiber_count, prev->fiber_count, 8); 1675 | ngx_conf_merge_size_value(conf->min_chunk_size, prev->min_chunk_size, 128 * 1024); 1676 | ngx_conf_merge_size_value(conf->max_chunk_size, prev->max_chunk_size, 512 * 1024); 1677 | ngx_conf_merge_size_value(conf->max_headers_size, prev->max_headers_size, 4 * 1024); 1678 | ngx_conf_merge_uint_value(conf->max_buffer_count, prev->max_buffer_count, 16); 1679 | ngx_conf_merge_value(conf->consistency_check_etag, prev->consistency_check_etag, 1); 1680 | ngx_conf_merge_value(conf->consistency_check_last_modified, prev->consistency_check_last_modified, 1); 1681 | 1682 | if (conf->content_length_cache_zone == NULL) 1683 | { 1684 | conf->content_length_cache_zone = prev->content_length_cache_zone; 1685 | } 1686 | 1687 | if (conf->min_chunk_size > conf->max_chunk_size) 1688 | { 1689 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 1690 | "\"parallel_min_chunk_size\" %uz cannot be greater than " 1691 | "\"parallel_max_chunk_size\" %uz", 1692 | conf->min_chunk_size, conf->max_chunk_size); 1693 | return NGX_CONF_ERROR; 1694 | } 1695 | 1696 | if (conf->max_buffer_count < conf->fiber_count) 1697 | { 1698 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 1699 | "\"parallel_max_buffer_count\" %ui cannot be less than " 1700 | "\"parallel_fiber_count\" %ui", 1701 | conf->max_buffer_count, conf->fiber_count); 1702 | return NGX_CONF_ERROR; 1703 | } 1704 | 1705 | return NGX_CONF_OK; 1706 | } 1707 | 1708 | 1709 | static char * 1710 | ngx_http_parallel_command(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 1711 | { 1712 | ngx_http_core_loc_conf_t *clcf; 1713 | ngx_str_t *field, *value; 1714 | 1715 | // set up handler 1716 | clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); 1717 | clcf->handler = ngx_http_parallel_handler; 1718 | 1719 | // save the uri prefix param 1720 | field = &((ngx_http_parallel_loc_conf_t*)conf)->uri_prefix; 1721 | 1722 | if (field->data) { 1723 | return "is duplicate"; 1724 | } 1725 | 1726 | value = cf->args->elts; 1727 | 1728 | *field = value[1]; 1729 | 1730 | return NGX_CONF_OK; 1731 | } 1732 | 1733 | static char * 1734 | ngx_http_parallel_cache_command(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 1735 | { 1736 | ngx_shm_zone_t **zone = (ngx_shm_zone_t **)((u_char*)conf + cmd->offset); 1737 | ngx_str_t *value; 1738 | ssize_t size; 1739 | 1740 | value = cf->args->elts; 1741 | 1742 | if (*zone != NULL) 1743 | { 1744 | return "is duplicate"; 1745 | } 1746 | 1747 | if (ngx_strcmp(value[1].data, "off") == 0) 1748 | { 1749 | *zone = NULL; 1750 | return NGX_CONF_OK; 1751 | } 1752 | 1753 | if (cf->args->nelts < 3) 1754 | { 1755 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 1756 | "size not specified in \"%V\"", &cmd->name); 1757 | return NGX_CONF_ERROR; 1758 | } 1759 | 1760 | size = ngx_parse_size(&value[2]); 1761 | if (size == NGX_ERROR) 1762 | { 1763 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 1764 | "invalid size %V", &value[2]); 1765 | return NGX_CONF_ERROR; 1766 | } 1767 | 1768 | *zone = ngx_fixed_buffer_cache_create_zone(cf, &value[1], size, 1769 | sizeof(off_t), &ngx_http_parallel_module); 1770 | if (*zone == NULL) 1771 | { 1772 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 1773 | "failed to create cache zone"); 1774 | return NGX_CONF_ERROR; 1775 | } 1776 | 1777 | return NGX_CONF_OK; 1778 | } 1779 | 1780 | static ngx_int_t 1781 | ngx_http_parallel_filter_init(ngx_conf_t *cf) 1782 | { 1783 | ngx_http_next_body_filter = ngx_http_top_body_filter; 1784 | ngx_http_top_body_filter = ngx_http_parallel_body_filter; 1785 | 1786 | return NGX_OK; 1787 | } 1788 | -------------------------------------------------------------------------------- /travis_build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -o nounset # Treat unset variables as an error 3 | 4 | NGINX_VERSION=1.8.0 5 | NGINX_URI="http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz" 6 | 7 | 8 | if [ ! -x "`which wget 2>/dev/null`" ];then 9 | echo "Need to install wget." 10 | exit 2 11 | fi 12 | mkdir -p /tmp/builddir/nginx-$NGINX_VERSION 13 | cp -r . /tmp/builddir/nginx-$NGINX_VERSION/nginx-parallel-module 14 | cd /tmp/builddir 15 | wget $NGINX_URI -O kaltura-nginx-$NGINX_VERSION.tar.gz 16 | tar zxvf kaltura-nginx-$NGINX_VERSION.tar.gz 17 | cd nginx-$NGINX_VERSION 18 | 19 | LD_LIBRARY_PATH=/opt/kaltura/ffmpeg-2.1.3/lib 20 | LIBRARY_PATH=/opt/kaltura/ffmpeg-2.1.3/lib 21 | C_INCLUDE_PATH=/opt/kaltura/ffmpeg-2.1.3/include 22 | export LD_LIBRARY_PATH LIBRARY_PATH C_INCLUDE_PATH 23 | 24 | ./configure \ 25 | --prefix=/etc/nginx \ 26 | --sbin-path=/sbin/nginx \ 27 | --conf-path=/etc/nginx/nginx.conf \ 28 | --error-log-path=/var/log/log/nginx/error.log \ 29 | --http-log-path=/var/log/log/nginx/access.log \ 30 | --pid-path=/var/log/run/nginx.pid \ 31 | --lock-path=/var/log/run/nginx.lock \ 32 | --http-client-body-temp-path=/var/log/cache/nginx/client_temp \ 33 | --http-proxy-temp-path=/var/log/cache/nginx/proxy_temp \ 34 | --http-fastcgi-temp-path=/var/log/cache/nginx/fastcgi_temp \ 35 | --http-uwsgi-temp-path=/var/log/cache/nginx/uwsgi_temp \ 36 | --http-scgi-temp-path=/var/log/cache/nginx/scgi_temp \ 37 | --with-http_ssl_module \ 38 | --with-http_realip_module \ 39 | --with-http_addition_module \ 40 | --with-http_sub_module \ 41 | --with-http_dav_module \ 42 | --with-http_flv_module \ 43 | --with-http_mp4_module \ 44 | --with-http_gunzip_module \ 45 | --with-http_gzip_static_module \ 46 | --with-http_random_index_module \ 47 | --with-http_secure_link_module \ 48 | --with-http_stub_status_module \ 49 | --with-http_auth_request_module \ 50 | --with-mail \ 51 | --with-mail_ssl_module \ 52 | --with-file-aio \ 53 | --with-ipv6 \ 54 | --with-debug \ 55 | --with-threads \ 56 | --add-module=./nginx-parallel-module \ 57 | $* 58 | make 59 | --------------------------------------------------------------------------------