├── .gitignore ├── LICENSE.txt ├── README.md ├── TinyWebServer.cpp ├── TinyWebServer.h ├── Unittest └── Unittest.ino └── examples ├── BlinkLed ├── BlinkLed.ino ├── README.txt ├── static │ ├── index.htm │ ├── jquery.js │ ├── lights.png │ ├── main.js │ └── style.css ├── upload.bat └── upload.sh ├── FileUpload ├── FileUpload.ino ├── README.txt ├── html │ ├── index.htm │ └── lava.jpg ├── upload.bat └── upload.sh ├── SimpleWebServer └── SimpleWebServer.ino └── WebServerSD └── WebServerSD.ino /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .DS_Store 3 | 4 | 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | GNU LESSER GENERAL PUBLIC LICENSE 3 | Version 2.1, February 1999 4 | 5 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 6 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 7 | Everyone is permitted to copy and distribute verbatim copies 8 | of this license document, but changing it is not allowed. 9 | 10 | [This is the first released version of the Lesser GPL. It also counts 11 | as the successor of the GNU Library Public License, version 2, hence 12 | the version number 2.1.] 13 | 14 | Preamble 15 | 16 | The licenses for most software are designed to take away your 17 | freedom to share and change it. By contrast, the GNU General Public 18 | Licenses are intended to guarantee your freedom to share and change 19 | free software--to make sure the software is free for all its users. 20 | 21 | This license, the Lesser General Public License, applies to some 22 | specially designated software packages--typically libraries--of the 23 | Free Software Foundation and other authors who decide to use it. You 24 | can use it too, but we suggest you first think carefully about whether 25 | this license or the ordinary General Public License is the better 26 | strategy to use in any particular case, based on the explanations below. 27 | 28 | When we speak of free software, we are referring to freedom of use, 29 | not price. Our General Public Licenses are designed to make sure that 30 | you have the freedom to distribute copies of free software (and charge 31 | for this service if you wish); that you receive source code or can get 32 | it if you want it; that you can change the software and use pieces of 33 | it in new free programs; and that you are informed that you can do 34 | these things. 35 | 36 | To protect your rights, we need to make restrictions that forbid 37 | distributors to deny you these rights or to ask you to surrender these 38 | rights. These restrictions translate to certain responsibilities for 39 | you if you distribute copies of the library or if you modify it. 40 | 41 | For example, if you distribute copies of the library, whether gratis 42 | or for a fee, you must give the recipients all the rights that we gave 43 | you. You must make sure that they, too, receive or can get the source 44 | code. If you link other code with the library, you must provide 45 | complete object files to the recipients, so that they can relink them 46 | with the library after making changes to the library and recompiling 47 | it. And you must show them these terms so they know their rights. 48 | 49 | We protect your rights with a two-step method: (1) we copyright the 50 | library, and (2) we offer you this license, which gives you legal 51 | permission to copy, distribute and/or modify the library. 52 | 53 | To protect each distributor, we want to make it very clear that 54 | there is no warranty for the free library. Also, if the library is 55 | modified by someone else and passed on, the recipients should know 56 | that what they have is not the original version, so that the original 57 | author's reputation will not be affected by problems that might be 58 | introduced by others. 59 | 60 | Finally, software patents pose a constant threat to the existence of 61 | any free program. We wish to make sure that a company cannot 62 | effectively restrict the users of a free program by obtaining a 63 | restrictive license from a patent holder. Therefore, we insist that 64 | any patent license obtained for a version of the library must be 65 | consistent with the full freedom of use specified in this license. 66 | 67 | Most GNU software, including some libraries, is covered by the 68 | ordinary GNU General Public License. This license, the GNU Lesser 69 | General Public License, applies to certain designated libraries, and 70 | is quite different from the ordinary General Public License. We use 71 | this license for certain libraries in order to permit linking those 72 | libraries into non-free programs. 73 | 74 | When a program is linked with a library, whether statically or using 75 | a shared library, the combination of the two is legally speaking a 76 | combined work, a derivative of the original library. The ordinary 77 | General Public License therefore permits such linking only if the 78 | entire combination fits its criteria of freedom. The Lesser General 79 | Public License permits more lax criteria for linking other code with 80 | the library. 81 | 82 | We call this license the "Lesser" General Public License because it 83 | does Less to protect the user's freedom than the ordinary General 84 | Public License. It also provides other free software developers Less 85 | of an advantage over competing non-free programs. These disadvantages 86 | are the reason we use the ordinary General Public License for many 87 | libraries. However, the Lesser license provides advantages in certain 88 | special circumstances. 89 | 90 | For example, on rare occasions, there may be a special need to 91 | encourage the widest possible use of a certain library, so that it becomes 92 | a de-facto standard. To achieve this, non-free programs must be 93 | allowed to use the library. A more frequent case is that a free 94 | library does the same job as widely used non-free libraries. In this 95 | case, there is little to gain by limiting the free library to free 96 | software only, so we use the Lesser General Public License. 97 | 98 | In other cases, permission to use a particular library in non-free 99 | programs enables a greater number of people to use a large body of 100 | free software. For example, permission to use the GNU C Library in 101 | non-free programs enables many more people to use the whole GNU 102 | operating system, as well as its variant, the GNU/Linux operating 103 | system. 104 | 105 | Although the Lesser General Public License is Less protective of the 106 | users' freedom, it does ensure that the user of a program that is 107 | linked with the Library has the freedom and the wherewithal to run 108 | that program using a modified version of the Library. 109 | 110 | The precise terms and conditions for copying, distribution and 111 | modification follow. Pay close attention to the difference between a 112 | "work based on the library" and a "work that uses the library". The 113 | former contains code derived from the library, whereas the latter must 114 | be combined with the library in order to run. 115 | 116 | GNU LESSER GENERAL PUBLIC LICENSE 117 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 118 | 119 | 0. This License Agreement applies to any software library or other 120 | program which contains a notice placed by the copyright holder or 121 | other authorized party saying it may be distributed under the terms of 122 | this Lesser General Public License (also called "this License"). 123 | Each licensee is addressed as "you". 124 | 125 | A "library" means a collection of software functions and/or data 126 | prepared so as to be conveniently linked with application programs 127 | (which use some of those functions and data) to form executables. 128 | 129 | The "Library", below, refers to any such software library or work 130 | which has been distributed under these terms. A "work based on the 131 | Library" means either the Library or any derivative work under 132 | copyright law: that is to say, a work containing the Library or a 133 | portion of it, either verbatim or with modifications and/or translated 134 | straightforwardly into another language. (Hereinafter, translation is 135 | included without limitation in the term "modification".) 136 | 137 | "Source code" for a work means the preferred form of the work for 138 | making modifications to it. For a library, complete source code means 139 | all the source code for all modules it contains, plus any associated 140 | interface definition files, plus the scripts used to control compilation 141 | and installation of the library. 142 | 143 | Activities other than copying, distribution and modification are not 144 | covered by this License; they are outside its scope. The act of 145 | running a program using the Library is not restricted, and output from 146 | such a program is covered only if its contents constitute a work based 147 | on the Library (independent of the use of the Library in a tool for 148 | writing it). Whether that is true depends on what the Library does 149 | and what the program that uses the Library does. 150 | 151 | 1. You may copy and distribute verbatim copies of the Library's 152 | complete source code as you receive it, in any medium, provided that 153 | you conspicuously and appropriately publish on each copy an 154 | appropriate copyright notice and disclaimer of warranty; keep intact 155 | all the notices that refer to this License and to the absence of any 156 | warranty; and distribute a copy of this License along with the 157 | Library. 158 | 159 | You may charge a fee for the physical act of transferring a copy, 160 | and you may at your option offer warranty protection in exchange for a 161 | fee. 162 | 163 | 2. You may modify your copy or copies of the Library or any portion 164 | of it, thus forming a work based on the Library, and copy and 165 | distribute such modifications or work under the terms of Section 1 166 | above, provided that you also meet all of these conditions: 167 | 168 | a) The modified work must itself be a software library. 169 | 170 | b) You must cause the files modified to carry prominent notices 171 | stating that you changed the files and the date of any change. 172 | 173 | c) You must cause the whole of the work to be licensed at no 174 | charge to all third parties under the terms of this License. 175 | 176 | d) If a facility in the modified Library refers to a function or a 177 | table of data to be supplied by an application program that uses 178 | the facility, other than as an argument passed when the facility 179 | is invoked, then you must make a good faith effort to ensure that, 180 | in the event an application does not supply such function or 181 | table, the facility still operates, and performs whatever part of 182 | its purpose remains meaningful. 183 | 184 | (For example, a function in a library to compute square roots has 185 | a purpose that is entirely well-defined independent of the 186 | application. Therefore, Subsection 2d requires that any 187 | application-supplied function or table used by this function must 188 | be optional: if the application does not supply it, the square 189 | root function must still compute square roots.) 190 | 191 | These requirements apply to the modified work as a whole. If 192 | identifiable sections of that work are not derived from the Library, 193 | and can be reasonably considered independent and separate works in 194 | themselves, then this License, and its terms, do not apply to those 195 | sections when you distribute them as separate works. But when you 196 | distribute the same sections as part of a whole which is a work based 197 | on the Library, the distribution of the whole must be on the terms of 198 | this License, whose permissions for other licensees extend to the 199 | entire whole, and thus to each and every part regardless of who wrote 200 | it. 201 | 202 | Thus, it is not the intent of this section to claim rights or contest 203 | your rights to work written entirely by you; rather, the intent is to 204 | exercise the right to control the distribution of derivative or 205 | collective works based on the Library. 206 | 207 | In addition, mere aggregation of another work not based on the Library 208 | with the Library (or with a work based on the Library) on a volume of 209 | a storage or distribution medium does not bring the other work under 210 | the scope of this License. 211 | 212 | 3. You may opt to apply the terms of the ordinary GNU General Public 213 | License instead of this License to a given copy of the Library. To do 214 | this, you must alter all the notices that refer to this License, so 215 | that they refer to the ordinary GNU General Public License, version 2, 216 | instead of to this License. (If a newer version than version 2 of the 217 | ordinary GNU General Public License has appeared, then you can specify 218 | that version instead if you wish.) Do not make any other change in 219 | these notices. 220 | 221 | Once this change is made in a given copy, it is irreversible for 222 | that copy, so the ordinary GNU General Public License applies to all 223 | subsequent copies and derivative works made from that copy. 224 | 225 | This option is useful when you wish to copy part of the code of 226 | the Library into a program that is not a library. 227 | 228 | 4. You may copy and distribute the Library (or a portion or 229 | derivative of it, under Section 2) in object code or executable form 230 | under the terms of Sections 1 and 2 above provided that you accompany 231 | it with the complete corresponding machine-readable source code, which 232 | must be distributed under the terms of Sections 1 and 2 above on a 233 | medium customarily used for software interchange. 234 | 235 | If distribution of object code is made by offering access to copy 236 | from a designated place, then offering equivalent access to copy the 237 | source code from the same place satisfies the requirement to 238 | distribute the source code, even though third parties are not 239 | compelled to copy the source along with the object code. 240 | 241 | 5. A program that contains no derivative of any portion of the 242 | Library, but is designed to work with the Library by being compiled or 243 | linked with it, is called a "work that uses the Library". Such a 244 | work, in isolation, is not a derivative work of the Library, and 245 | therefore falls outside the scope of this License. 246 | 247 | However, linking a "work that uses the Library" with the Library 248 | creates an executable that is a derivative of the Library (because it 249 | contains portions of the Library), rather than a "work that uses the 250 | library". The executable is therefore covered by this License. 251 | Section 6 states terms for distribution of such executables. 252 | 253 | When a "work that uses the Library" uses material from a header file 254 | that is part of the Library, the object code for the work may be a 255 | derivative work of the Library even though the source code is not. 256 | Whether this is true is especially significant if the work can be 257 | linked without the Library, or if the work is itself a library. The 258 | threshold for this to be true is not precisely defined by law. 259 | 260 | If such an object file uses only numerical parameters, data 261 | structure layouts and accessors, and small macros and small inline 262 | functions (ten lines or less in length), then the use of the object 263 | file is unrestricted, regardless of whether it is legally a derivative 264 | work. (Executables containing this object code plus portions of the 265 | Library will still fall under Section 6.) 266 | 267 | Otherwise, if the work is a derivative of the Library, you may 268 | distribute the object code for the work under the terms of Section 6. 269 | Any executables containing that work also fall under Section 6, 270 | whether or not they are linked directly with the Library itself. 271 | 272 | 6. As an exception to the Sections above, you may also combine or 273 | link a "work that uses the Library" with the Library to produce a 274 | work containing portions of the Library, and distribute that work 275 | under terms of your choice, provided that the terms permit 276 | modification of the work for the customer's own use and reverse 277 | engineering for debugging such modifications. 278 | 279 | You must give prominent notice with each copy of the work that the 280 | Library is used in it and that the Library and its use are covered by 281 | this License. You must supply a copy of this License. If the work 282 | during execution displays copyright notices, you must include the 283 | copyright notice for the Library among them, as well as a reference 284 | directing the user to the copy of this License. Also, you must do one 285 | of these things: 286 | 287 | a) Accompany the work with the complete corresponding 288 | machine-readable source code for the Library including whatever 289 | changes were used in the work (which must be distributed under 290 | Sections 1 and 2 above); and, if the work is an executable linked 291 | with the Library, with the complete machine-readable "work that 292 | uses the Library", as object code and/or source code, so that the 293 | user can modify the Library and then relink to produce a modified 294 | executable containing the modified Library. (It is understood 295 | that the user who changes the contents of definitions files in the 296 | Library will not necessarily be able to recompile the application 297 | to use the modified definitions.) 298 | 299 | b) Use a suitable shared library mechanism for linking with the 300 | Library. A suitable mechanism is one that (1) uses at run time a 301 | copy of the library already present on the user's computer system, 302 | rather than copying library functions into the executable, and (2) 303 | will operate properly with a modified version of the library, if 304 | the user installs one, as long as the modified version is 305 | interface-compatible with the version that the work was made with. 306 | 307 | c) Accompany the work with a written offer, valid for at 308 | least three years, to give the same user the materials 309 | specified in Subsection 6a, above, for a charge no more 310 | than the cost of performing this distribution. 311 | 312 | d) If distribution of the work is made by offering access to copy 313 | from a designated place, offer equivalent access to copy the above 314 | specified materials from the same place. 315 | 316 | e) Verify that the user has already received a copy of these 317 | materials or that you have already sent this user a copy. 318 | 319 | For an executable, the required form of the "work that uses the 320 | Library" must include any data and utility programs needed for 321 | reproducing the executable from it. However, as a special exception, 322 | the materials to be distributed need not include anything that is 323 | normally distributed (in either source or binary form) with the major 324 | components (compiler, kernel, and so on) of the operating system on 325 | which the executable runs, unless that component itself accompanies 326 | the executable. 327 | 328 | It may happen that this requirement contradicts the license 329 | restrictions of other proprietary libraries that do not normally 330 | accompany the operating system. Such a contradiction means you cannot 331 | use both them and the Library together in an executable that you 332 | distribute. 333 | 334 | 7. You may place library facilities that are a work based on the 335 | Library side-by-side in a single library together with other library 336 | facilities not covered by this License, and distribute such a combined 337 | library, provided that the separate distribution of the work based on 338 | the Library and of the other library facilities is otherwise 339 | permitted, and provided that you do these two things: 340 | 341 | a) Accompany the combined library with a copy of the same work 342 | based on the Library, uncombined with any other library 343 | facilities. This must be distributed under the terms of the 344 | Sections above. 345 | 346 | b) Give prominent notice with the combined library of the fact 347 | that part of it is a work based on the Library, and explaining 348 | where to find the accompanying uncombined form of the same work. 349 | 350 | 8. You may not copy, modify, sublicense, link with, or distribute 351 | the Library except as expressly provided under this License. Any 352 | attempt otherwise to copy, modify, sublicense, link with, or 353 | distribute the Library is void, and will automatically terminate your 354 | rights under this License. However, parties who have received copies, 355 | or rights, from you under this License will not have their licenses 356 | terminated so long as such parties remain in full compliance. 357 | 358 | 9. You are not required to accept this License, since you have not 359 | signed it. However, nothing else grants you permission to modify or 360 | distribute the Library or its derivative works. These actions are 361 | prohibited by law if you do not accept this License. Therefore, by 362 | modifying or distributing the Library (or any work based on the 363 | Library), you indicate your acceptance of this License to do so, and 364 | all its terms and conditions for copying, distributing or modifying 365 | the Library or works based on it. 366 | 367 | 10. Each time you redistribute the Library (or any work based on the 368 | Library), the recipient automatically receives a license from the 369 | original licensor to copy, distribute, link with or modify the Library 370 | subject to these terms and conditions. You may not impose any further 371 | restrictions on the recipients' exercise of the rights granted herein. 372 | You are not responsible for enforcing compliance by third parties with 373 | this License. 374 | 375 | 11. If, as a consequence of a court judgment or allegation of patent 376 | infringement or for any other reason (not limited to patent issues), 377 | conditions are imposed on you (whether by court order, agreement or 378 | otherwise) that contradict the conditions of this License, they do not 379 | excuse you from the conditions of this License. If you cannot 380 | distribute so as to satisfy simultaneously your obligations under this 381 | License and any other pertinent obligations, then as a consequence you 382 | may not distribute the Library at all. For example, if a patent 383 | license would not permit royalty-free redistribution of the Library by 384 | all those who receive copies directly or indirectly through you, then 385 | the only way you could satisfy both it and this License would be to 386 | refrain entirely from distribution of the Library. 387 | 388 | If any portion of this section is held invalid or unenforceable under any 389 | particular circumstance, the balance of the section is intended to apply, 390 | and the section as a whole is intended to apply in other circumstances. 391 | 392 | It is not the purpose of this section to induce you to infringe any 393 | patents or other property right claims or to contest validity of any 394 | such claims; this section has the sole purpose of protecting the 395 | integrity of the free software distribution system which is 396 | implemented by public license practices. Many people have made 397 | generous contributions to the wide range of software distributed 398 | through that system in reliance on consistent application of that 399 | system; it is up to the author/donor to decide if he or she is willing 400 | to distribute software through any other system and a licensee cannot 401 | impose that choice. 402 | 403 | This section is intended to make thoroughly clear what is believed to 404 | be a consequence of the rest of this License. 405 | 406 | 12. If the distribution and/or use of the Library is restricted in 407 | certain countries either by patents or by copyrighted interfaces, the 408 | original copyright holder who places the Library under this License may add 409 | an explicit geographical distribution limitation excluding those countries, 410 | so that distribution is permitted only in or among countries not thus 411 | excluded. In such case, this License incorporates the limitation as if 412 | written in the body of this License. 413 | 414 | 13. The Free Software Foundation may publish revised and/or new 415 | versions of the Lesser General Public License from time to time. 416 | Such new versions will be similar in spirit to the present version, 417 | but may differ in detail to address new problems or concerns. 418 | 419 | Each version is given a distinguishing version number. If the Library 420 | specifies a version number of this License which applies to it and 421 | "any later version", you have the option of following the terms and 422 | conditions either of that version or of any later version published by 423 | the Free Software Foundation. If the Library does not specify a 424 | license version number, you may choose any version ever published by 425 | the Free Software Foundation. 426 | 427 | 14. If you wish to incorporate parts of the Library into other free 428 | programs whose distribution conditions are incompatible with these, 429 | write to the author to ask for permission. For software which is 430 | copyrighted by the Free Software Foundation, write to the Free 431 | Software Foundation; we sometimes make exceptions for this. Our 432 | decision will be guided by the two goals of preserving the free status 433 | of all derivatives of our free software and of promoting the sharing 434 | and reuse of software generally. 435 | 436 | NO WARRANTY 437 | 438 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 439 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 440 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 441 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 442 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 443 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 444 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 445 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 446 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 447 | 448 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 449 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 450 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 451 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 452 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 453 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 454 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 455 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 456 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 457 | DAMAGES. 458 | 459 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Date: 2015-03-06 14:58:18 2 | Arduino TinyWebServer 3 | 4 | A small web server for Arduino. 5 | 6 | Check out this video demonstrating what you can do with it: 7 | 8 | http://www.youtube.com/watch?v=qZXKk6nCYuM 9 | 10 | You can read up on TinyWebServer's history here: 11 | 12 | http://www.webweavertech.com/ovidiu/weblog/archives/000484.html 13 | 14 | http://www.webweavertech.com/ovidiu/weblog/archives/000477.html 15 | 16 | The library is licensed under the terms of LGPL 2.1. Which means 17 | you're free to use it in your projects (including commercial ones) as 18 | long as you're sending back the changes you make to the library. 19 | 20 | External dependencies 21 | ==================== 22 | 23 | TinyWebServer depends on the external library Flash version 5.0, which 24 | is found here: 25 | 26 | http://arduiniana.org/libraries/flash/ 27 | 28 | Make sure you dowload the Flash library and install it in your 29 | Arduino's `libraries` directory, as described in this document: 30 | 31 | http://arduino.cc/en/Guide/Libraries 32 | 33 | If you're using a version of Arduino IDE newer than 1.5, you need to 34 | modify the `Flash.h` file to include the following lines just after 35 | the `#include ` line: 36 | 37 | ``` 38 | #if ARDUINO >= 150 39 | typedef char prog_char __attribute__((__progmem__)); 40 | #endif 41 | ``` 42 | 43 | If you're using an ARM Cortex M0-based board, such as the Arduino Zero 44 | or the MKR1000, you need to modify the `Flash.h` file to include the 45 | following: 46 | 47 | ``` 48 | #if ARDUINO_ARCH_SAMD 49 | extern char* strncpy_P(char* dest, const char* src, int size); 50 | #endif 51 | ``` 52 | 53 | Basic web server 54 | ================ 55 | 56 | To make use of the TinyWebServer library, you need to include the 57 | following your sketch: 58 | 59 | #include 60 | #include 61 | #include 62 | #include 63 | 64 | TWS is implemented by the TinyWebServer class. The constructor method 65 | takes two arguments. The first one is a list of handlers, functions to 66 | be invoked when a particular URL is requested by an HTTP client. The 67 | second one is a list of HTTP header names that are needed by the 68 | implementation of your handlers. More on these later. 69 | 70 | An HTTP handler is a simple function that takes as argument a 71 | reference to the TinyWebServer object. When you create the 72 | TinyWebServer class, you need to pass in the handlers for the various 73 | URLs. Here is a simple example of a web server with a single handler. 74 | 75 | static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 76 | 77 | boolean index_handler(TinyWebServer& web_server) { 78 | web_server.send_error_code(200); 79 | web_server << F("

Hello World!

\n"); 80 | return true; 81 | } 82 | 83 | TinyWebServer::PathHandler handlers[] = { 84 | // Register the index_handler for GET requests on / 85 | {"/", TinyWebServer::GET, &index_handler }, 86 | {NULL}, // The array has to be NULL terminated this way 87 | }; 88 | 89 | // Create an instance of the web server. No HTTP headers are requested 90 | // by the HTTP request handlers. 91 | TinyWebServer web = TinyWebServer(handlers, NULL); 92 | 93 | void setup() { 94 | Serial.begin(115200); 95 | EthernetDHCP.begin(mac); 96 | web.begin(); 97 | } 98 | 99 | void loop() { 100 | EthernetDHCP.maintain(); 101 | web.process(); 102 | } 103 | 104 | In the loop() function we need the call to the process() to make sure 105 | HTTP requests are serviced. If there is no new request, the method 106 | returns immediately. Otherwise the process() method blocks until the 107 | request is handled. 108 | 109 | For a complete working example look in 110 | TinyWebServer/example/SimpleWebServer. 111 | 112 | Serving files from the SD card 113 | ============================== 114 | 115 | Now that we've seen the basics, let's see how we can extend this web 116 | server to serve files stored on the SD card. The idea is to register a 117 | handler that serves any URLs. Once the handler is invoked, it 118 | interprets the URL path as a file name on the SD card and returns 119 | that. 120 | 121 | boolean file_handler(TinyWebServer& web_server) { 122 | char* filename = TinyWebServer::get_file_from_path(web_server.get_path()); 123 | if (!filename) { 124 | web_server.send_error_code(404); 125 | web_server << "Could not parse URL"; 126 | } else { 127 | TinyWebServer::MimeType mime_type 128 | = TinyWebServer::get_mime_type_from_filename(filename); 129 | web_server.send_error_code(mime_type, 200); 130 | if (file.open(filename, O_READ)) { 131 | web_server.send_file(file); 132 | file.close(); 133 | } else { 134 | web_server << "Could not find file: " << filename << "\n"; 135 | } 136 | free(filename); 137 | } 138 | return true; 139 | } 140 | 141 | We can now register this in the handlers array: 142 | 143 | TinyWebServer::PathHandler handlers[] = { 144 | {"/" "*", TinyWebServer::GET, &file_handler }, 145 | {NULL}, 146 | }; 147 | 148 | Note how the URL for the HTTP request is specified. We want it to 149 | be /*, very much like a regular expression. However Arduino's IDE 150 | preprocessor has a bug in how it handles /* inside strings. By 151 | specifying the string as "/" "*" we avoid the bug, while letting the 152 | compiler optimize and concatenate the two strings into a single one. 153 | 154 | The * works only at the end of a URL, anywhere else it would be 155 | interpreted as part of the URL. If the * is at the end of the URL, the 156 | code in TinyWebServer assumes the handler can process requests that 157 | match the URL prefix. For example, if the URL string was /html/* then 158 | any URL starting with /html/ would be handled by the specified 159 | handler. In our case, since we specified /*, any URL starting with / 160 | (except for the top level / URL) will invoke the specified handler. 161 | 162 | Uploading files to the web server and store them on SD card's file system 163 | ========================================================================= 164 | 165 | Now wouldn't it be nice to update Arduino's Web server files using 166 | HTTP? This way we can focus on building the actual interface with the 167 | hardware, and provide just enough HTTP handlers to interact with 168 | it. After we implement a minimal user interface, we can iterate it 169 | without having to remove the SD card from the embedded project, copy 170 | the HTML, JavaScript and/or image files on a computer, and plug it 171 | back in. We could do this remotely from the computer, using a simple 172 | script. 173 | 174 | TinyWebServer provides a simple file upload HTTP handler that uses the 175 | HTTP 1.0 PUT method. This allows you to implement an Ajax interface 176 | using XMLHttpRequest or simply use a tool like curl to implement file 177 | uploads. 178 | 179 | Here's how you add file uploads to your Arduino web server: 180 | 181 | TinyWebServer::PathHandler handlers[] = { 182 | // `put_handler' is defined in TinyWebServer 183 | {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler }, 184 | {"/" "*", TinyWebServer::GET, &file_handler }, 185 | {NULL}, 186 | 187 | Note that the order in which you declare the handlers is 188 | important. The URLs are matched in the order in which they are 189 | declared. 190 | 191 | This is where the headers array mentioned before comes into 192 | picture. The put_handler makes use of the Content-Length. To avoid 193 | unnecessary work and minimize precious memory usage, TinyWebServer 194 | does not do any header processing unless it's instructed. To do so, 195 | you need to declare an array of header names your handlers are 196 | interested in. In this case, we need to add Content-Length. 197 | 198 | const char* headers[] = { 199 | "Content-Length", 200 | NULL 201 | }; 202 | 203 | And we now initialize the instance of TinyWebServer like this: 204 | 205 | TinyWebServer web = TinyWebServer(handlers, headers); 206 | 207 | The put_handler method is really generic, it doesn't actually 208 | implement the code to write the file to disk. Instead the method 209 | relies on a user provided function that implements the actual 210 | logic. This allows you to use a different file system implementation 211 | than Fat16 or do something totally different than write the file to 212 | disk. 213 | 214 | The user provided function take 4 parameters. The first is a reference 215 | to the TinyWebServer instance. The second is a PutAction enum which 216 | could be either START, WRITE or END. START and END are called exactly 217 | once during a PUT handler's execution, while WRITE is called multiple 218 | times. Each time the function is called with the WRITE param, the 219 | third and fourth parameters are set to a buffer and a number of bytes 220 | in this buffer that should be used. 221 | 222 | Here is a small example of a user provided function that writes the 223 | PUT request's content to a file: 224 | 225 | void file_uploader_handler(TinyWebServer& web_server, 226 | TinyWebPutHandler::PutAction action, 227 | char* buffer, int size) { 228 | static uint32_t start_time; 229 | 230 | switch (action) { 231 | case TinyWebPutHandler::START: 232 | start_time = millis(); 233 | if (!file.isOpen()) { 234 | // File is not opened, create it. First obtain the desired name 235 | // from the request path. 236 | char* fname = web_server.get_file_from_path(web_server.get_path()); 237 | if (fname) { 238 | Serial << "Creating " << fname << "\n"; 239 | file.open(fname, O_CREAT | O_WRITE | O_TRUNC); 240 | free(fname); 241 | } 242 | } 243 | break; 244 | 245 | case TinyWebPutHandler::WRITE: 246 | if (file.isOpen()) { 247 | file.write(buffer, size); 248 | } 249 | break; 250 | 251 | case TinyWebPutHandler::END: 252 | file.sync(); 253 | Serial << "Wrote " << file.fileSize() << " bytes in " 254 | << millis() - start_time << " millis\n"; 255 | file.close(); 256 | } 257 | } 258 | 259 | To activate this user provided function, assign its address to 260 | put_handler_fn, like this: 261 | 262 | void setup() { 263 | // ... 264 | 265 | // Assign our function to `upload_handler_fn'. 266 | TinyWebPutHandler::put_handler_fn = file_uploader_handler; 267 | 268 | // ... 269 | } 270 | 271 | You can now test uploading a file using curl: 272 | 273 | *Note that since the handler in the source looks like this: 274 | 275 | {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler } 276 | 277 | you must ensure that the path '/upload/' is in your submitted URL 278 | 279 | 280 | curl -0 -T index.htm http://my-arduino-ip-address/upload/ 281 | 282 | For a complete working example of the file upload and serving web 283 | server, look in TinyWebServer/examples/FileUpload. 284 | 285 | Advanced topic: persistent HTTP connections 286 | =========================================== 287 | 288 | Sometimes it's useful to have an HTTP client start a request. For 289 | example, I need to be able to enter an IR learning process. This means 290 | that I cannot afford TinyWebServer's process() to block while serving 291 | my /learn request that initiated the IR learning process. Instead I 292 | want the handler of the /learn request to set a variable in the code 293 | that indicates that IR learning is active, and then return 294 | immediately. 295 | 296 | If you noticed the HTTP handlers return a boolean. If the returned 297 | value is true, as it was the case in our examples above, the 298 | connection to the HTTP client is closed immediately. If the returned 299 | value is false the connection is left open. Your handler should save 300 | the Client object handling the HTTP connection with the original 301 | request. Your code becomes responsible with closing it when it's no 302 | longer needed. 303 | 304 | To obtain the Client object, use the get_client() method while in the 305 | HTTP handler. You can write asynchronously to the client, to update it 306 | with the state of the web server. 307 | 308 | In my remotely controlled projection screen application, I have 309 | another handler on /cancel that closes the /learn client 310 | forcibly. Otherwise the /learn's Client connection is closed at the 311 | end of the IR learning procedure. Since the Ethernet shield only 312 | allows for 4 maximum HTTP clients open at the same time (because of 4 313 | maximum client sockets), in my application I allow only one /learn 314 | handler to be active at any given time. 315 | -------------------------------------------------------------------------------- /TinyWebServer.cpp: -------------------------------------------------------------------------------- 1 | // -*- c++ -*- 2 | // 3 | // Copyright 2010 Ovidiu Predescu 4 | // Date: May 2010 5 | // 6 | // Updated: 08-JAN-2012 for Arduno IDE 1.0 by 7 | // Updated: 29-MAR-2013 replacing strtoul with parseHexChar by 8 | // 9 | // TinyWebServer for Arduino. 10 | // 11 | // The DEBUG flag will enable serial console logging in this library 12 | // By default Debugging to the Serial console is OFF. 13 | // This ensures that any scripts using the Serial port are not corrupted 14 | // by the tinywebserver libraries debugging messages. 15 | // 16 | // To ENABLE debugging set the following: 17 | // DEBUG 1 and ENSURE that you have configured the serial port in the 18 | // main Arduino script. 19 | // 20 | // There is an overall size increase of about 340 bytes in code size 21 | // when the debugging is enabled and debugging lines are preceded by 'TWS:' 22 | 23 | #define DEBUG 0 24 | 25 | // 10 milliseconds read timeout 26 | #define READ_TIMEOUT 10 27 | 28 | #include "Arduino.h" 29 | 30 | extern "C" { 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | } 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | #include "TinyWebServer.h" 43 | 44 | // Temporary buffer. 45 | static char buffer[160]; 46 | 47 | FLASH_STRING(mime_types, 48 | "HTM*text/html|" 49 | "TXT*text/plain|" 50 | "CSS*text/css|" 51 | "XML*text/xml|" 52 | "JS*text/javascript|" 53 | 54 | "GIF*image/gif|" 55 | "JPG*image/jpeg|" 56 | "PNG*image/png|" 57 | "ICO*image/vnd.microsoft.icon|" 58 | 59 | "MP3*audio/mpeg|" 60 | ); 61 | 62 | void *malloc_check(size_t size) { 63 | void* r = malloc(size); 64 | #if DEBUG 65 | if (!r) { 66 | Serial << F("TWS:No space for malloc: " ); Serial.println(size, DEC); 67 | } 68 | #endif 69 | return r; 70 | } 71 | 72 | // Offset for text/html in `mime_types' above. 73 | static const TinyWebServer::MimeType text_html_content_type = 4; 74 | 75 | TinyWebServer::TinyWebServer(PathHandler handlers[], 76 | const char** headers, 77 | const int port) 78 | : handlers_(handlers), 79 | server_(EthernetServer(port)), 80 | path_(NULL), 81 | request_type_(UNKNOWN_REQUEST), 82 | client_(EthernetClient(255)) { 83 | if (headers) { 84 | int size = 0; 85 | for (int i = 0; headers[i]; i++) { 86 | size++; 87 | } 88 | headers_ = (HeaderValue*)malloc_check(sizeof(HeaderValue) * (size + 1)); 89 | if (headers_) { 90 | for (int i = 0; i < size; i++) { 91 | headers_[i].header = headers[i]; 92 | headers_[i].value = NULL; 93 | } 94 | headers_[size].header = NULL; 95 | } 96 | } 97 | } 98 | 99 | void TinyWebServer::begin() { 100 | server_.begin(); 101 | } 102 | 103 | // Process headers. 104 | boolean TinyWebServer::process_headers() { 105 | if (headers_) { 106 | // First clear the header values from the previous HTTP request. 107 | for (int i = 0; headers_[i].header; i++) { 108 | if (headers_[i].value) { 109 | free(headers_[i].value); 110 | // Ensure the pointer is cleared once the memory is freed. 111 | headers_[i].value = NULL; 112 | } 113 | } 114 | } 115 | 116 | enum State { 117 | ERROR, 118 | START_LINE, 119 | HEADER_NAME, 120 | HEADER_VALUE, 121 | HEADER_VALUE_SKIP_INITIAL_SPACES, 122 | HEADER_IGNORE_VALUE, 123 | END_HEADERS, 124 | }; 125 | State state = START_LINE; 126 | 127 | char ch; 128 | int pos; 129 | const char* header; 130 | uint32_t start_time = millis(); 131 | while (1) { 132 | if (should_stop_processing()) { 133 | return false; 134 | } 135 | if (millis() - start_time > READ_TIMEOUT) { 136 | return false; 137 | } 138 | if (!read_next_char(client_, (uint8_t*)&ch)) { 139 | continue; 140 | } 141 | start_time = millis(); 142 | #if DEBUG 143 | Serial.print(ch); 144 | #endif 145 | switch (state) { 146 | case START_LINE: 147 | if (ch == '\r') { 148 | break; 149 | } else if (ch == '\n') { 150 | state = END_HEADERS; 151 | } else if (isalnum(ch) || ch == '-') { 152 | pos = 0; 153 | buffer[pos++] = ch; 154 | state = HEADER_NAME; 155 | } else { 156 | state = ERROR; 157 | } 158 | break; 159 | 160 | case HEADER_NAME: 161 | if (pos + 1 >= sizeof(buffer)) { 162 | state = ERROR; 163 | break; 164 | } 165 | if (ch == ':') { 166 | buffer[pos] = 0; 167 | header = buffer; 168 | if (is_requested_header(&header)) { 169 | state = HEADER_VALUE_SKIP_INITIAL_SPACES; 170 | } else { 171 | state = HEADER_IGNORE_VALUE; 172 | } 173 | pos = 0; 174 | } else if (isalnum(ch) || ch == '-') { 175 | buffer[pos++] = ch; 176 | } else { 177 | state = ERROR; 178 | break; 179 | } 180 | break; 181 | 182 | case HEADER_VALUE_SKIP_INITIAL_SPACES: 183 | if (pos + 1 >= sizeof(buffer)) { 184 | state = ERROR; 185 | break; 186 | } 187 | if (ch != ' ') { 188 | buffer[pos++] = ch; 189 | state = HEADER_VALUE; 190 | } 191 | break; 192 | 193 | case HEADER_VALUE: 194 | if (pos + 1 >= sizeof(buffer)) { 195 | state = ERROR; 196 | break; 197 | } 198 | if (ch == '\n') { 199 | buffer[pos] = 0; 200 | if (!assign_header_value(header, buffer)) { 201 | state = ERROR; 202 | break; 203 | } 204 | state = START_LINE; 205 | } else { 206 | if (ch != '\r') { 207 | buffer[pos++] = ch; 208 | } 209 | } 210 | break; 211 | 212 | case HEADER_IGNORE_VALUE: 213 | if (ch == '\n') { 214 | state = START_LINE; 215 | } 216 | break; 217 | 218 | default: 219 | break; 220 | } 221 | 222 | if (state == END_HEADERS) { 223 | break; 224 | } 225 | if (state == ERROR) { 226 | return false; 227 | } 228 | } 229 | return true; 230 | } 231 | 232 | void TinyWebServer::process() { 233 | client_ = server_.available(); 234 | if (!client_.connected() || !client_.available()) { 235 | return; 236 | } 237 | 238 | boolean is_complete = get_line(buffer, sizeof(buffer)); 239 | if (!buffer[0]) { 240 | return; 241 | } 242 | #if DEBUG 243 | Serial << F("TWS:New request: "); 244 | Serial.println(buffer); 245 | #endif 246 | if (!is_complete) { 247 | // The requested path is too long. 248 | send_error_code(414); 249 | client_.stop(); 250 | return; 251 | } 252 | 253 | char* request_type_str = get_field(buffer, 0); 254 | request_type_ = UNKNOWN_REQUEST; 255 | if (!strcmp("GET", request_type_str)) { 256 | request_type_ = GET; 257 | } else if (!strcmp("POST", request_type_str)) { 258 | request_type_ = POST; 259 | } else if (!strcmp("PUT", request_type_str)) { 260 | request_type_ = PUT; 261 | } else if (!strcmp("DELETE", request_type_str)) { 262 | request_type_ = DELETE; 263 | } 264 | 265 | path_ = get_field(buffer, 1); 266 | 267 | // Process the headers. 268 | if (!process_headers()) { 269 | // Malformed header line. 270 | send_error_code(417); 271 | client_.stop(); 272 | } 273 | // Header processing finished. Identify the handler to call. 274 | 275 | boolean should_close = true; 276 | boolean found = false; 277 | for (int i = 0; handlers_[i].path; i++) { 278 | int len = strlen(handlers_[i].path); 279 | boolean exact_match = !strcmp(path_, handlers_[i].path); 280 | boolean regex_match = false; 281 | if (handlers_[i].path[len - 1] == '*') { 282 | regex_match = !strncmp(path_, handlers_[i].path, len - 1); 283 | } 284 | if ((exact_match || regex_match) 285 | && (handlers_[i].type == ANY || handlers_[i].type == request_type_)) { 286 | found = true; 287 | should_close = (handlers_[i].handler)(*this); 288 | break; 289 | } 290 | } 291 | 292 | if (!found) { 293 | send_error_code(404); 294 | // (*this) << F("URL not found: "); 295 | // client_->print(path_); 296 | // client_->println(); 297 | } 298 | if (should_close) { 299 | client_.stop(); 300 | } 301 | 302 | free(path_); 303 | free(request_type_str); 304 | } 305 | 306 | boolean TinyWebServer::is_requested_header(const char** header) { 307 | if (!headers_) { 308 | return false; 309 | } 310 | for (int i = 0; headers_[i].header; i++) { 311 | if (!strcmp(*header, headers_[i].header)) { 312 | *header = headers_[i].header; 313 | return true; 314 | } 315 | } 316 | return false; 317 | } 318 | 319 | boolean TinyWebServer::assign_header_value(const char* header, char* value) { 320 | if (!headers_) { 321 | return false; 322 | } 323 | boolean found = false; 324 | for (int i = 0; headers_[i].header; i++) { 325 | // Use pointer equality, since `header' must be the pointer 326 | // inside headers_. 327 | if (header == headers_[i].header) { 328 | headers_[i].value = (char*)malloc_check(strlen(value) + 1); 329 | if (!headers_[i].value) { 330 | return false; 331 | } 332 | strcpy(headers_[i].value, value); 333 | found = true; 334 | break; 335 | } 336 | } 337 | return found; 338 | } 339 | 340 | FLASH_STRING(content_type_msg, "Content-Type: "); 341 | 342 | void TinyWebServer::send_error_code(Client& client, int code) { 343 | #if DEBUG 344 | Serial << F("TWS:Returning "); 345 | Serial.println(code, DEC); 346 | #endif 347 | client << F("HTTP/1.1 "); 348 | client.print(code, DEC); 349 | client << F(" OK\r\n"); 350 | if (code != 200) { 351 | end_headers(client); 352 | } 353 | } 354 | 355 | void TinyWebServer::send_content_type(MimeType mime_type) { 356 | client_ << content_type_msg; 357 | 358 | char ch; 359 | int i = mime_type; 360 | while ((ch = mime_types[i++]) != '|') { 361 | client_.print(ch); 362 | } 363 | 364 | client_.println(); 365 | } 366 | 367 | void TinyWebServer::send_content_type(const char* content_type) { 368 | client_ << content_type_msg; 369 | client_.println(content_type); 370 | } 371 | 372 | const char* TinyWebServer::get_path() { return path_; } 373 | 374 | const TinyWebServer::HttpRequestType TinyWebServer::get_type() { 375 | return request_type_; 376 | } 377 | 378 | const char* TinyWebServer::get_header_value(const char* name) { 379 | if (!headers_) { 380 | return NULL; 381 | } 382 | for (int i = 0; headers_[i].header; i++) { 383 | if (!strcmp(headers_[i].header, name)) { 384 | return headers_[i].value; 385 | } 386 | } 387 | return NULL; 388 | } 389 | 390 | int parseHexChar(char ch) { 391 | if (isdigit(ch)) { 392 | return ch - '0'; 393 | } 394 | ch = tolower(ch); 395 | if (ch >= 'a' && ch <= 'e') { 396 | return ch - 'a' + 10; 397 | } 398 | return 0; 399 | } 400 | 401 | char* TinyWebServer::decode_url_encoded(const char* s) { 402 | if (!s) { 403 | return NULL; 404 | } 405 | char* r = (char*)malloc_check(strlen(s) + 1); 406 | if (!r){ 407 | return NULL; 408 | } 409 | char* r2 = r; 410 | const char* p = s; 411 | while (*s && (p = strchr(s, '%'))) { 412 | if (p - s) { 413 | memcpy(r2, s, p - s); 414 | r2 += (p - s); 415 | } 416 | // If the remaining number of characters is less than 3, we cannot 417 | // have a complete escape sequence. Break early. 418 | if (strlen(p) < 3) { 419 | // Move the new beginning to the value of p. 420 | s = p; 421 | break; 422 | } 423 | uint8_t r = parseHexChar(*(p + 1)) << 4 | parseHexChar(*(p + 2)); 424 | *r2++ = r; 425 | p += 3; 426 | 427 | // Move the new beginning to the value of p. 428 | s = p; 429 | } 430 | // Copy whatever is left of the string in the result. 431 | int len = strlen(s); 432 | if (len > 0) { 433 | strncpy(r2, s, len); 434 | } 435 | // Append the 0 terminator. 436 | *(r2 + len) = 0; 437 | 438 | return r; 439 | } 440 | 441 | char* TinyWebServer::get_file_from_path(const char* path) { 442 | // Obtain the last path component. 443 | const char* encoded_fname = strrchr(path, '/'); 444 | if (!encoded_fname) { 445 | return NULL; 446 | } else { 447 | // Skip past the '/'. 448 | encoded_fname++; 449 | } 450 | char* decoded = decode_url_encoded(encoded_fname); 451 | if (!decoded) { 452 | return NULL; 453 | } 454 | for (char* p = decoded; *p; p++) { 455 | *p = toupper(*p); 456 | } 457 | return decoded; 458 | } 459 | 460 | TinyWebServer::MimeType TinyWebServer::get_mime_type_from_filename( 461 | const char* filename) { 462 | MimeType r = text_html_content_type; 463 | if (!filename) { 464 | return r; 465 | } 466 | 467 | char* ext = strrchr(filename, '.'); 468 | if (ext) { 469 | // We found an extension. Skip past the '.' 470 | ext++; 471 | 472 | char ch; 473 | int i = 0; 474 | while (i < mime_types.length()) { 475 | // Compare the extension. 476 | char* p = ext; 477 | ch = mime_types[i]; 478 | while (*p && ch != '*' && toupper(*p) == ch) { 479 | p++; i++; 480 | ch = mime_types[i]; 481 | } 482 | if (!*p && ch == '*') { 483 | // We reached the end of the extension while checking 484 | // equality with a MIME type: we have a match. Increment i 485 | // to reach past the '*' char, and assign it to `mime_type'. 486 | r = ++i; 487 | break; 488 | } else { 489 | // Skip past the the '|' character indicating the end of a 490 | // MIME type. 491 | while (mime_types[i++] != '|') 492 | ; 493 | } 494 | } 495 | } 496 | return r; 497 | } 498 | 499 | void TinyWebServer::send_file(SdFile& file) { 500 | size_t size; 501 | while ((size = file.read(buffer, sizeof(buffer))) > 0) { 502 | if (!client_.connected()) { 503 | break; 504 | } 505 | write((uint8_t*)buffer, size); 506 | } 507 | } 508 | 509 | size_t TinyWebServer::write(uint8_t c) { 510 | client_.write(c); 511 | } 512 | 513 | size_t TinyWebServer::write(const char *str) { 514 | client_.write(str); 515 | } 516 | 517 | size_t TinyWebServer::write(const uint8_t *buffer, size_t size) { 518 | client_.write(buffer, size); 519 | } 520 | 521 | boolean TinyWebServer::read_next_char(Client& client, uint8_t* ch) { 522 | if (!client.available()) { 523 | return false; 524 | } else { 525 | *ch = client.read(); 526 | return true; 527 | } 528 | } 529 | 530 | boolean TinyWebServer::get_line(char* buffer, int size) { 531 | int i = 0; 532 | char ch; 533 | 534 | buffer[0] = 0; 535 | for (; i < size - 1; i++) { 536 | if (!read_next_char(client_, (uint8_t*)&ch)) { 537 | continue; 538 | } 539 | if (ch == '\n') { 540 | break; 541 | } 542 | buffer[i] = ch; 543 | } 544 | buffer[i] = 0; 545 | return i < size - 1; 546 | } 547 | 548 | // Returns a newly allocated string containing the field number `which`. 549 | // The first field's index is 0. 550 | // The caller is responsible for freeing the returned value. 551 | char* TinyWebServer::get_field(const char* buffer, int which) { 552 | char* field = NULL; 553 | boolean prev_is_space = false; 554 | int i = 0; 555 | int field_no = 0; 556 | int size = strlen(buffer); 557 | 558 | // Locate the field we need. A field is defined as an area of 559 | // non-space characters delimited by one or more space characters. 560 | for (; field_no < which; field_no++) { 561 | // Skip over space characters 562 | while (i < size && isspace(buffer[i])) { 563 | i++; 564 | } 565 | // Skip over non-space characters. 566 | while (i < size && !isspace(buffer[i])) { 567 | i++; 568 | } 569 | } 570 | 571 | // Now we identify the end of the field that we want. 572 | // Skip over space characters. 573 | while (i < size && isspace(buffer[i])) { 574 | i++; 575 | } 576 | 577 | if (field_no == which && i < size) { 578 | // Now identify where the field ends. 579 | int j = i; 580 | while (j < size && !isspace(buffer[j])) { 581 | j++; 582 | } 583 | 584 | field = (char*) malloc_check(j - i + 1); 585 | if (!field) { 586 | return NULL; 587 | } 588 | memcpy(field, buffer + i, j - i); 589 | field[j - i] = 0; 590 | } 591 | return field; 592 | } 593 | 594 | // The PUT handler. 595 | 596 | namespace TinyWebPutHandler { 597 | 598 | HandlerFn put_handler_fn = NULL; 599 | 600 | // Fills in `buffer' by reading up to `num_bytes'. 601 | // Returns the number of characters read. 602 | int read_chars(TinyWebServer& web_server, Client& client, 603 | uint8_t* buffer, int size) { 604 | uint8_t ch; 605 | int pos; 606 | for (pos = 0; pos < size && web_server.read_next_char(client, &ch); pos++) { 607 | buffer[pos] = ch; 608 | } 609 | return pos; 610 | } 611 | 612 | boolean put_handler(TinyWebServer& web_server) { 613 | web_server.send_error_code(200); 614 | web_server.end_headers(); 615 | 616 | const char* length_str = web_server.get_header_value("Content-Length"); 617 | long length = atol(length_str); 618 | uint32_t start_time = 0; 619 | boolean watchdog_start = false; 620 | 621 | EthernetClient client = web_server.get_client(); 622 | 623 | if (put_handler_fn) { 624 | (*put_handler_fn)(web_server, START, NULL, length); 625 | } 626 | 627 | uint32_t i; 628 | for (i = 0; i < length && client.connected();) { 629 | int16_t size = read_chars(web_server, client, (uint8_t*)buffer, 64); 630 | if (!size) { 631 | if (watchdog_start) { 632 | if (millis() - start_time > 30000) { 633 | // Exit if there has been zero data from connected client 634 | // for more than 30 seconds. 635 | #if DEBUG 636 | Serial << F("TWS:There has been no data for >30 Sec.\n"); 637 | #endif 638 | break; 639 | } 640 | } else { 641 | // We have hit an empty buffer, start the watchdog. 642 | start_time = millis(); 643 | watchdog_start = true; 644 | } 645 | continue; 646 | } 647 | i += size; 648 | // Ensure we re-start the watchdog if we get ANY data input. 649 | watchdog_start = false; 650 | 651 | if (put_handler_fn) { 652 | (*put_handler_fn)(web_server, WRITE, buffer, size); 653 | } 654 | } 655 | if (put_handler_fn) { 656 | (*put_handler_fn)(web_server, END, NULL, 0); 657 | } 658 | 659 | return true; 660 | } 661 | 662 | }; 663 | -------------------------------------------------------------------------------- /TinyWebServer.h: -------------------------------------------------------------------------------- 1 | // -*- c++ -*- 2 | // 3 | // Copyright 2010 Ovidiu Predescu 4 | // Date: May, June 2010 5 | // 6 | // Updated: 08-JAN-2012 for Arduno IDE 1.0 by 7 | // 8 | // TinyWebServer for Arduino. 9 | 10 | #ifndef __WEB_SERVER_H__ 11 | #define __WEB_SERVER_H__ 12 | 13 | #include 14 | 15 | class SdFile; 16 | class TinyWebServer; 17 | 18 | namespace TinyWebPutHandler { 19 | enum PutAction { 20 | START, 21 | WRITE, 22 | END 23 | }; 24 | 25 | typedef void (*HandlerFn)(TinyWebServer& web_server, 26 | PutAction action, 27 | char* buffer, int size); 28 | 29 | // An HTTP handler that knows how to handle file uploads using the 30 | // PUT method. Set the `put_handler_fn' variable below to your own 31 | // function to handle the characters of the uploaded function. 32 | boolean put_handler(TinyWebServer& web_server); 33 | extern HandlerFn put_handler_fn; 34 | }; 35 | 36 | class TinyWebServer : public Print { 37 | public: 38 | // An HTTP path handler. The handler function takes the path it 39 | // registered for as argument, and the Client object to handle the 40 | // response. 41 | // 42 | // The function should return true if it finished handling the request 43 | // and the connection should be closed. 44 | typedef boolean (*WebHandlerFn)(TinyWebServer& web_server); 45 | 46 | enum HttpRequestType { 47 | UNKNOWN_REQUEST, 48 | GET, 49 | HEAD, 50 | POST, 51 | PUT, 52 | DELETE, 53 | ANY, 54 | }; 55 | 56 | // An identifier for a MIME type. The number is opaque to a human, 57 | // but it's really an offset in the `mime_types' array. 58 | typedef uint16_t MimeType; 59 | 60 | typedef struct { 61 | const char* path; 62 | HttpRequestType type; 63 | WebHandlerFn handler; 64 | } PathHandler; 65 | 66 | // Initialize the web server using a NULL terminated array of path 67 | // handlers, and a NULL terminated array of headers the handlers are 68 | // interested in. 69 | // 70 | // NOTE: Make sure the header names are all lowercase. 71 | TinyWebServer(PathHandler handlers[], const char** headers, 72 | const int port=80); 73 | 74 | // Call this method to start the HTTP server 75 | void begin(); 76 | 77 | // Handles a possible HTTP request. It will return immediately if no 78 | // client has connected. Otherwise the request is handled 79 | // synchronously. 80 | // 81 | // Call this method from the main loop() function to have the Web 82 | // server handle incoming requests. 83 | void process(); 84 | 85 | // Sends the HTTP status code to the connect HTTP client. 86 | void send_error_code(int code) { 87 | send_error_code(client_, code); 88 | } 89 | static void send_error_code(Client& client, int code); 90 | 91 | void send_content_type(MimeType mime_type); 92 | void send_content_type(const char* content_type); 93 | 94 | // Call this method to indicate the end of the headers. 95 | inline void end_headers() { client_.println(); } 96 | static inline void end_headers(Client& client) { client.println(); } 97 | 98 | // void send_error_code(MimeType mime_type, int code); 99 | // void send_error_code(const char* content_type, int code); 100 | 101 | const char* get_path(); 102 | const HttpRequestType get_type(); 103 | const char* get_header_value(const char* header); 104 | EthernetClient& get_client() { return client_; } 105 | 106 | // Processes the HTTP headers and assigns values to the requested 107 | // ones in headers_. Returns true when successful, false in case of 108 | // errors. 109 | boolean process_headers(); 110 | 111 | // Helper methods 112 | 113 | // Assumes `s' is an HTTP encoded URL, replaces all the escape 114 | // characters in it and returns the unencoded version. For example 115 | // for "/index%2Ehtm", this method returns "index.htm". 116 | // 117 | // The returned string must be free()d by the caller. 118 | static char* decode_url_encoded(const char* s); 119 | 120 | // Assumes the last component of the URL path is a file 121 | // name. Returns the file name in upper case, ready to passed to 122 | // SdFile's open() method. 123 | // 124 | // In addition to the file name, it sets `mime_type' to an identifier 125 | // 126 | // The returned string must be free()d by the caller. 127 | static char* get_file_from_path(const char* path); 128 | 129 | // Guesses a MIME type based on the extension of `filename'. If none 130 | // could be guessed, the equivalent of text/html is returned. 131 | static MimeType get_mime_type_from_filename(const char* filename); 132 | 133 | // Sends the contents of `file' to the currently connected 134 | // client. The file must be opened in read mode. 135 | // 136 | // This is mainly an optimization to reuse the internal static 137 | // buffer used by this class, which saves us some RAM. 138 | void send_file(SdFile& file); 139 | 140 | // These methods write directly in the response stream of the 141 | // connected client 142 | virtual size_t write(uint8_t c); 143 | virtual size_t write(const char *str); 144 | virtual size_t write(const uint8_t *buffer, size_t size); 145 | 146 | // Some methods used for testing purposes 147 | 148 | // Returns true if the HTTP request processing should be stopped. 149 | virtual boolean should_stop_processing() { return !client_.connected();} 150 | 151 | // Reads a character from the request's input stream. Returns true 152 | // if the character could be read, false otherwise. 153 | virtual boolean read_next_char(Client& client, uint8_t* ch); 154 | 155 | protected: 156 | // Returns the field number `which' from buffer. Fields are 157 | // separated by spaces. Should be a private method, but made public 158 | // so it can be tested. 159 | static char* get_field(const char* buffer, int which); 160 | 161 | private: 162 | // The path handlers 163 | PathHandler* handlers_; 164 | 165 | typedef struct { 166 | const char* header; 167 | char* value; 168 | } HeaderValue; 169 | 170 | // The headers 171 | HeaderValue* headers_; 172 | 173 | // The TCP/IP server we use. 174 | EthernetServer server_; 175 | 176 | char* path_; 177 | HttpRequestType request_type_; 178 | EthernetClient client_; 179 | 180 | // Reads a line from the HTTP request sent by an HTTP client. The 181 | // line is put in `buffer' and up to `size' characters are written 182 | // in it. 183 | boolean get_line(char* buffer, int size); 184 | 185 | // Returns true if the header is marked as requested in the headers_ 186 | // array. As a side effect, the pointer to the actual header is made 187 | // to point to the one in the headers_ array. 188 | boolean is_requested_header(const char** header); 189 | 190 | boolean assign_header_value(const char* header, char* value); 191 | }; 192 | 193 | #endif /* __WEB_SERVER_H__ */ 194 | -------------------------------------------------------------------------------- /Unittest/Unittest.ino: -------------------------------------------------------------------------------- 1 | // -*- c++ -*- 2 | // 3 | // Copyright 2010 Ovidiu Predescu 4 | // Date: June 2010 5 | // Updated: 08-JAN-2012 for Arduno IDE 1.0 by 6 | // 7 | 8 | extern "C" { 9 | #include 10 | } 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | 19 | class TinyWebServerTest : public TinyWebServer { 20 | public: 21 | TinyWebServerTest(PathHandler handlers[], const char** headers, 22 | const _FLASH_STRING& content) 23 | : TinyWebServer(handlers, headers), 24 | content_(content), 25 | pos_(0) {} 26 | 27 | static char* get_field_public(const char* buffer, int which) { 28 | return get_field(buffer, which); 29 | } 30 | 31 | protected: 32 | const _FLASH_STRING& content_; 33 | uint16_t pos_; 34 | 35 | // Returns true if the HTTP request processing should be stopped. 36 | virtual boolean should_stop_processing() { return false; } 37 | 38 | // Reads a character from the request's input stream. Returns true 39 | // if the character could be read, false otherwise. 40 | virtual boolean read_next_char(Client& client, uint8_t* ch) { 41 | if (pos_ == content_.length()) { 42 | return false; 43 | } else { 44 | *ch = content_[pos_++]; 45 | return true; 46 | } 47 | } 48 | }; 49 | 50 | int failures = 0; 51 | 52 | void expect_str_eq(const char* s1, char* s2, boolean free_s2 = true) { 53 | if (strcmp(s1, s2) != 0) { 54 | Serial << F("FAIL: expect ") << s1 << F(", got ") << s2 << "\n"; 55 | failures++; 56 | } 57 | if (free_s2) { 58 | free(s2); 59 | } 60 | } 61 | 62 | void expect_num_eq(uint32_t n1, uint32_t n2) { 63 | if (n1 != n2) { 64 | Serial << F("FAIL: expect ") << n1 << F(", got ") << n2 << "\n"; 65 | failures++; 66 | } 67 | } 68 | 69 | void expect_true(boolean r) { 70 | if (!r) { 71 | Serial << F("FAIL: expected true, got false\n"); 72 | failures++; 73 | } 74 | } 75 | 76 | void test_decode_url_encoded() { 77 | expect_str_eq("index.htm", TinyWebServer::decode_url_encoded("index%2Ehtm")); 78 | expect_str_eq("index.", TinyWebServer::decode_url_encoded("index%2E")); 79 | expect_str_eq("index%2", TinyWebServer::decode_url_encoded("index%2")); 80 | expect_str_eq(".index", TinyWebServer::decode_url_encoded("%2Eindex")); 81 | expect_str_eq("", TinyWebServer::decode_url_encoded("")); 82 | 83 | // Invalid escape sequences 84 | expect_str_eq("%GEhtm", TinyWebServer::decode_url_encoded("%GEhtm")); 85 | expect_str_eq("%%Ehtm", TinyWebServer::decode_url_encoded("%%Ehtm")); 86 | } 87 | 88 | void test_get_file_from_path() { 89 | expect_str_eq("", TinyWebServer::get_file_from_path("/")); 90 | expect_str_eq("INDEX.HTM", TinyWebServer::get_file_from_path("/index%2Ehtm")); 91 | expect_str_eq("INDEX.HTM", 92 | TinyWebServer::get_file_from_path("/upload/index%2Ehtm")); 93 | expect_str_eq("INDEX.HTM", 94 | TinyWebServer::get_file_from_path("/a/b/index%2Ehtm")); 95 | } 96 | 97 | void test_get_mime_type_from_filename() { 98 | uint16_t codes[9]; 99 | uint16_t html_code; 100 | 101 | codes[0] = TinyWebServer::get_mime_type_from_filename("index.htm"); 102 | html_code = TinyWebServer::get_mime_type_from_filename("INDEX.HTM"); 103 | expect_num_eq(codes[0], html_code); 104 | 105 | codes[1] = TinyWebServer::get_mime_type_from_filename("file.txt"); 106 | html_code = TinyWebServer::get_mime_type_from_filename("FILE.TXT"); 107 | expect_num_eq(codes[1], html_code); 108 | 109 | codes[2] = TinyWebServer::get_mime_type_from_filename("style.css"); 110 | html_code = TinyWebServer::get_mime_type_from_filename("STYLE.CSS"); 111 | expect_num_eq(codes[2], html_code); 112 | 113 | codes[3] = TinyWebServer::get_mime_type_from_filename("file.xml"); 114 | html_code = TinyWebServer::get_mime_type_from_filename("FILE.XML"); 115 | expect_num_eq(codes[3], html_code); 116 | 117 | codes[4] = TinyWebServer::get_mime_type_from_filename("img.gif"); 118 | html_code = TinyWebServer::get_mime_type_from_filename("IMG.GIF"); 119 | expect_num_eq(codes[4], html_code); 120 | 121 | codes[5] = TinyWebServer::get_mime_type_from_filename("img.jpg"); 122 | html_code = TinyWebServer::get_mime_type_from_filename("IMG.JPG"); 123 | expect_num_eq(codes[5], html_code); 124 | 125 | codes[6] = TinyWebServer::get_mime_type_from_filename("img.png"); 126 | html_code = TinyWebServer::get_mime_type_from_filename("IMG.PNG"); 127 | expect_num_eq(codes[6], html_code); 128 | 129 | codes[7] = TinyWebServer::get_mime_type_from_filename("img.ico"); 130 | html_code = TinyWebServer::get_mime_type_from_filename("IMG.ICO"); 131 | expect_num_eq(codes[7], html_code); 132 | 133 | codes[8] = TinyWebServer::get_mime_type_from_filename("p.mp3"); 134 | html_code = TinyWebServer::get_mime_type_from_filename("P.MP3"); 135 | expect_num_eq(codes[8], html_code); 136 | 137 | for (int i = 0; i < 8; i++) { 138 | if (codes[i] >= codes[i + 1]) { 139 | Serial << F("FAIL: expect ") << codes[i] << ", got " << codes[i+1] <<"\n"; 140 | failures++; 141 | } 142 | } 143 | } 144 | 145 | void test_get_field() { 146 | { 147 | char b[] = "GET / HTTP/1.0"; 148 | expect_str_eq("GET", TinyWebServerTest::get_field_public(b, 0)); 149 | expect_str_eq("/", TinyWebServerTest::get_field_public(b, 1)); 150 | expect_str_eq("HTTP/1.0", 151 | TinyWebServerTest::get_field_public(b, 2)); 152 | } 153 | 154 | { 155 | char b[] = " GET / HTTP/1.0 "; 156 | expect_str_eq("GET", TinyWebServerTest::get_field_public(b, 0)); 157 | expect_str_eq("/", TinyWebServerTest::get_field_public(b, 1)); 158 | expect_str_eq("HTTP/1.0", TinyWebServerTest::get_field_public(b, 2)); 159 | } 160 | 161 | { 162 | char b[] = "GET / HTTP/1.0"; 163 | expect_str_eq(NULL, TinyWebServerTest::get_field_public(b, 3)); 164 | } 165 | } 166 | 167 | void test_process_headers() { 168 | FLASH_STRING(content, 169 | "User-Agent: curl/7.19.7\r\n" 170 | "Host: arduino\r\n" 171 | "Accept: */" "*\r\n" 172 | "Content-Length: 49209\r\n" 173 | "\r\n" 174 | ); 175 | const char* headers[] = { 176 | "Content-Length", 177 | NULL 178 | }; 179 | 180 | TinyWebServerTest web(NULL, headers, content); 181 | expect_true(web.process_headers()); 182 | expect_str_eq("49209", (char*)web.get_header_value("Content-Length"), 183 | false /* don't free the second argument */); 184 | expect_str_eq(NULL, (char*)web.get_header_value("Host"), 185 | false /* don't free the second argument */); 186 | expect_str_eq(NULL, (char*)web.get_header_value("random-header"), 187 | false /* don't free the second argument */); 188 | } 189 | 190 | void test_process_broken_headers() { 191 | FLASH_STRING(content, 192 | "User-Agent curl/7.19.7\r\n" 193 | ); 194 | 195 | TinyWebServerTest web(NULL, NULL, content); 196 | expect_true(!web.process_headers()); 197 | } 198 | 199 | void setup() { 200 | Serial.begin(115200); 201 | Serial << F("Free RAM: ") << FreeRam() << "\n"; 202 | 203 | test_decode_url_encoded(); 204 | test_get_file_from_path(); 205 | test_get_mime_type_from_filename(); 206 | test_get_field(); 207 | test_process_headers(); 208 | test_process_broken_headers(); 209 | 210 | if (!failures) { 211 | Serial << F("\nSUCCESS\n"); 212 | } 213 | } 214 | 215 | void loop() { 216 | } 217 | -------------------------------------------------------------------------------- /examples/BlinkLed/BlinkLed.ino: -------------------------------------------------------------------------------- 1 | // -*- c++ -*- 2 | // 3 | // Copyright 2010 Ovidiu Predescu 4 | // Date: December 2010 5 | // Updated: 08-JAN-2012 for Arduno IDE 1.0 by 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /****************VALUES YOU CHANGE*************/ 16 | // The LED attached to PIN X on an Arduino board. 17 | const int LEDPIN = 7; 18 | 19 | // pin 4 is the SPI select pin for the SDcard 20 | const int SD_CS = 4; 21 | 22 | // pin 10 is the SPI select pin for the Ethernet 23 | const int ETHER_CS = 10; 24 | 25 | // Don't forget to modify the IP to an available one on your home network 26 | byte ip[] = { 192, 168, 5, 177 }; 27 | /*********************************************/ 28 | 29 | static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 30 | 31 | // The initial state of the LED 32 | int ledState = LOW; 33 | 34 | void setLedEnabled(boolean state) { 35 | ledState = state; 36 | digitalWrite(LEDPIN, ledState); 37 | } 38 | 39 | inline boolean getLedState() { return ledState; } 40 | 41 | boolean file_handler(TinyWebServer& web_server); 42 | boolean blink_led_handler(TinyWebServer& web_server); 43 | boolean led_status_handler(TinyWebServer& web_server); 44 | boolean index_handler(TinyWebServer& web_server); 45 | 46 | TinyWebServer::PathHandler handlers[] = { 47 | // Work around Arduino's IDE preprocessor bug in handling /* inside 48 | // strings. 49 | // 50 | // `put_handler' is defined in TinyWebServer 51 | {"/", TinyWebServer::GET, &index_handler }, 52 | {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler }, 53 | {"/blinkled", TinyWebServer::POST, &blink_led_handler }, 54 | {"/ledstatus" "*", TinyWebServer::GET, &led_status_handler }, 55 | {"/" "*", TinyWebServer::GET, &file_handler }, 56 | {NULL}, 57 | }; 58 | 59 | const char* headers[] = { 60 | "Content-Length", 61 | NULL 62 | }; 63 | 64 | TinyWebServer web = TinyWebServer(handlers, headers); 65 | 66 | boolean has_filesystem = true; 67 | Sd2Card card; 68 | SdVolume volume; 69 | SdFile root; 70 | SdFile file; 71 | 72 | void send_file_name(TinyWebServer& web_server, const char* filename) { 73 | if (!filename) { 74 | web_server.send_error_code(404); 75 | web_server << F("Could not parse URL"); 76 | } else { 77 | TinyWebServer::MimeType mime_type 78 | = TinyWebServer::get_mime_type_from_filename(filename); 79 | web_server.send_error_code(200); 80 | web_server.send_content_type(mime_type); 81 | web_server.end_headers(); 82 | if (file.open(&root, filename, O_READ)) { 83 | Serial << F("Read file "); Serial.println(filename); 84 | web_server.send_file(file); 85 | file.close(); 86 | } else { 87 | web_server << F("Could not find file: ") << filename << "\n"; 88 | } 89 | } 90 | } 91 | 92 | boolean file_handler(TinyWebServer& web_server) { 93 | char* filename = TinyWebServer::get_file_from_path(web_server.get_path()); 94 | send_file_name(web_server, filename); 95 | free(filename); 96 | return true; 97 | } 98 | 99 | boolean blink_led_handler(TinyWebServer& web_server) { 100 | web_server.send_error_code(200); 101 | web_server.send_content_type("text/plain"); 102 | web_server.end_headers(); 103 | // Reverse the state of the LED. 104 | setLedEnabled(!getLedState()); 105 | Client& client = web_server.get_client(); 106 | if (client.available()) { 107 | char ch = (char)client.read(); 108 | if (ch == '0') { 109 | setLedEnabled(false); 110 | } else if (ch == '1') { 111 | setLedEnabled(true); 112 | } 113 | } 114 | return true; 115 | } 116 | 117 | boolean led_status_handler(TinyWebServer& web_server) { 118 | web_server.send_error_code(200); 119 | web_server.send_content_type("text/plain"); 120 | web_server.end_headers(); 121 | Client& client = web_server.get_client(); 122 | client.println(getLedState(), DEC); 123 | return true; 124 | } 125 | 126 | boolean index_handler(TinyWebServer& web_server) { 127 | send_file_name(web_server, "INDEX.HTM"); 128 | return true; 129 | } 130 | 131 | void file_uploader_handler(TinyWebServer& web_server, 132 | TinyWebPutHandler::PutAction action, 133 | char* buffer, int size) { 134 | static uint32_t start_time; 135 | static uint32_t total_size; 136 | 137 | switch (action) { 138 | case TinyWebPutHandler::START: 139 | start_time = millis(); 140 | total_size = 0; 141 | if (!file.isOpen()) { 142 | // File is not opened, create it. First obtain the desired name 143 | // from the request path. 144 | char* fname = web_server.get_file_from_path(web_server.get_path()); 145 | if (fname) { 146 | Serial << F("Creating ") << fname << "\n"; 147 | file.open(&root, fname, O_CREAT | O_WRITE | O_TRUNC); 148 | free(fname); 149 | } 150 | } 151 | break; 152 | 153 | case TinyWebPutHandler::WRITE: 154 | if (file.isOpen()) { 155 | file.write(buffer, size); 156 | total_size += size; 157 | } 158 | break; 159 | 160 | case TinyWebPutHandler::END: 161 | file.sync(); 162 | Serial << F("Wrote ") << file.fileSize() << F(" bytes in ") 163 | << millis() - start_time << F(" millis (received ") 164 | << total_size << F(" bytes)\n"); 165 | file.close(); 166 | } 167 | } 168 | 169 | void setup() { 170 | Serial.begin(115200); 171 | Serial << F("Free RAM: ") << FreeRam() << "\n"; 172 | 173 | pinMode(LEDPIN, OUTPUT); 174 | setLedEnabled(false); 175 | 176 | pinMode(SS_PIN, OUTPUT); // set the SS pin as an output 177 | // (necessary to keep the board as 178 | // master and not SPI slave) 179 | digitalWrite(SS_PIN, HIGH); // and ensure SS is high 180 | 181 | // Ensure we are in a consistent state after power-up or a reset 182 | // button These pins are standard for the Arduino w5100 Rev 3 183 | // ethernet board They may need to be re-jigged for different boards 184 | pinMode(ETHER_CS, OUTPUT); // Set the CS pin as an output 185 | digitalWrite(ETHER_CS, HIGH); // Turn off the W5100 chip! (wait for 186 | // configuration) 187 | pinMode(SD_CS, OUTPUT); // Set the SDcard CS pin as an output 188 | digitalWrite(SD_CS, HIGH); // Turn off the SD card! (wait for 189 | // configuration) 190 | 191 | // initialize the SD card. 192 | Serial << F("Setting up SD card...\n"); 193 | // Pass over the speed and Chip select for the SD card 194 | if (!card.init(SPI_FULL_SPEED, SD_CS)) { 195 | Serial << F("card failed\n"); 196 | has_filesystem = false; 197 | } 198 | // initialize a FAT volume. 199 | if (!volume.init(&card)) { 200 | Serial << F("vol.init failed!\n"); 201 | has_filesystem = false; 202 | } 203 | if (!root.openRoot(&volume)) { 204 | Serial << F("openRoot failed"); 205 | has_filesystem = false; 206 | } 207 | 208 | if (has_filesystem) { 209 | // Assign our function to `upload_handler_fn'. 210 | TinyWebPutHandler::put_handler_fn = file_uploader_handler; 211 | } 212 | 213 | // Initialize the Ethernet. 214 | Serial << F("Setting up the Ethernet card...\n"); 215 | Ethernet.begin(mac, ip); 216 | 217 | // Start the web server. 218 | Serial << F("Web server starting...\n"); 219 | web.begin(); 220 | 221 | Serial << F("Ready to accept HTTP requests.\n"); 222 | } 223 | 224 | void loop() { 225 | if (has_filesystem) { 226 | web.process(); 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /examples/BlinkLed/README.txt: -------------------------------------------------------------------------------- 1 | To get this example running, make sure you modify the IP address 2 | inside the BlinkLed.ino and upload.sh files to match an unused IP 3 | address in your network. 4 | 5 | Connect an LED with a series resistor between pin 7 and Gnd of 6 | your Arduino. 7 | Connect LED's GND to a Gnd pin on your Arduino, the other terminal 8 | of the LED via the resistor should be on Pin 7. 9 | 10 | 11 | See the video below for how it works: 12 | http://www.youtube.com/watch?v=qZXKk6nCYuM 13 | 14 | Then run the upload.sh script to upload the files in the static 15 | directory to your Arduino web server. Point a browser to the IP 16 | address you specified above. 17 | 18 | If you're on Windows, make sure you install curl first on your 19 | machine. Then open a terminal and run curl like below for each of the 20 | files in the static/ directory. Replace $ARDUINO with the IP address 21 | of your Arduino and $file with the name of the file you want to 22 | upload. 23 | 24 | curl -0 -T $file http://$ARDUINO/upload/ 25 | 26 | *Note that since the handler in the 'BlinkLed.ino' looks like this: 27 | 28 | {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler } 29 | 30 | you must ensure that the path '/upload/' is in your submitted URL. 31 | 32 | If you're a Windows developer, I'd appreciate if you could write a 33 | small batch file that does the equivalent of update.sh. 34 | 35 | About 36 | ===== 37 | The images were obtained from: 38 | 39 | http://www.clker.com/clipart-light-bulb-led-off.html 40 | http://www.clker.com/clipart-light-bulb-led-on.html 41 | 42 | Prepared in Photoshop by me, then sprited using: 43 | 44 | http://spritegen.website-performance.org/ 45 | 46 | -------------------------------------------------------------------------------- /examples/BlinkLed/static/index.htm: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | TinyWebServer LED control 16 | 17 | 18 | 19 |
20 |
21 |
22 |
23 |
24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /examples/BlinkLed/static/jquery.js: -------------------------------------------------------------------------------- 1 | /*! jQuery v1.7 jquery.com | jquery.org/license */ 2 | (function(a,b){function cA(a){return f.isWindow(a)?a:a.nodeType===9?a.defaultView||a.parentWindow:!1}function cx(a){if(!cm[a]){var b=c.body,d=f("<"+a+">").appendTo(b),e=d.css("display");d.remove();if(e==="none"||e===""){cn||(cn=c.createElement("iframe"),cn.frameBorder=cn.width=cn.height=0),b.appendChild(cn);if(!co||!cn.createElement)co=(cn.contentWindow||cn.contentDocument).document,co.write((c.compatMode==="CSS1Compat"?"":"")+""),co.close();d=co.createElement(a),co.body.appendChild(d),e=f.css(d,"display"),b.removeChild(cn)}cm[a]=e}return cm[a]}function cw(a,b){var c={};f.each(cs.concat.apply([],cs.slice(0,b)),function(){c[this]=a});return c}function cv(){ct=b}function cu(){setTimeout(cv,0);return ct=f.now()}function cl(){try{return new a.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}}function ck(){try{return new a.XMLHttpRequest}catch(b){}}function ce(a,c){a.dataFilter&&(c=a.dataFilter(c,a.dataType));var d=a.dataTypes,e={},g,h,i=d.length,j,k=d[0],l,m,n,o,p;for(g=1;g0){c!=="border"&&f.each(e,function(){c||(d-=parseFloat(f.css(a,"padding"+this))||0),c==="margin"?d+=parseFloat(f.css(a,c+this))||0:d-=parseFloat(f.css(a,"border"+this+"Width"))||0});return d+"px"}d=bB(a,b,b);if(d<0||d==null)d=a.style[b]||0;d=parseFloat(d)||0,c&&f.each(e,function(){d+=parseFloat(f.css(a,"padding"+this))||0,c!=="padding"&&(d+=parseFloat(f.css(a,"border"+this+"Width"))||0),c==="margin"&&(d+=parseFloat(f.css(a,c+this))||0)});return d+"px"}function br(a,b){b.src?f.ajax({url:b.src,async:!1,dataType:"script"}):f.globalEval((b.text||b.textContent||b.innerHTML||"").replace(bi,"/*$0*/")),b.parentNode&&b.parentNode.removeChild(b)}function bq(a){var b=(a.nodeName||"").toLowerCase();b==="input"?bp(a):b!=="script"&&typeof a.getElementsByTagName!="undefined"&&f.grep(a.getElementsByTagName("input"),bp)}function bp(a){if(a.type==="checkbox"||a.type==="radio")a.defaultChecked=a.checked}function bo(a){return typeof a.getElementsByTagName!="undefined"?a.getElementsByTagName("*"):typeof a.querySelectorAll!="undefined"?a.querySelectorAll("*"):[]}function bn(a,b){var c;if(b.nodeType===1){b.clearAttributes&&b.clearAttributes(),b.mergeAttributes&&b.mergeAttributes(a),c=b.nodeName.toLowerCase();if(c==="object")b.outerHTML=a.outerHTML;else if(c!=="input"||a.type!=="checkbox"&&a.type!=="radio"){if(c==="option")b.selected=a.defaultSelected;else if(c==="input"||c==="textarea")b.defaultValue=a.defaultValue}else a.checked&&(b.defaultChecked=b.checked=a.checked),b.value!==a.value&&(b.value=a.value);b.removeAttribute(f.expando)}}function bm(a,b){if(b.nodeType===1&&!!f.hasData(a)){var c,d,e,g=f._data(a),h=f._data(b,g),i=g.events;if(i){delete h.handle,h.events={};for(c in i)for(d=0,e=i[c].length;d=0===c})}function V(a){return!a||!a.parentNode||a.parentNode.nodeType===11}function N(){return!0}function M(){return!1}function n(a,b,c){var d=b+"defer",e=b+"queue",g=b+"mark",h=f._data(a,d);h&&(c==="queue"||!f._data(a,e))&&(c==="mark"||!f._data(a,g))&&setTimeout(function(){!f._data(a,e)&&!f._data(a,g)&&(f.removeData(a,d,!0),h.fire())},0)}function m(a){for(var b in a){if(b==="data"&&f.isEmptyObject(a[b]))continue;if(b!=="toJSON")return!1}return!0}function l(a,c,d){if(d===b&&a.nodeType===1){var e="data-"+c.replace(k,"-$1").toLowerCase();d=a.getAttribute(e);if(typeof d=="string"){try{d=d==="true"?!0:d==="false"?!1:d==="null"?null:f.isNumeric(d)?parseFloat(d):j.test(d)?f.parseJSON(d):d}catch(g){}f.data(a,c,d)}else d=b}return d}function h(a){var b=g[a]={},c,d;a=a.split(/\s+/);for(c=0,d=a.length;c)[^>]*$|#([\w\-]*)$)/,j=/\S/,k=/^\s+/,l=/\s+$/,m=/\d/,n=/^<(\w+)\s*\/?>(?:<\/\1>)?$/,o=/^[\],:{}\s]*$/,p=/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,q=/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,r=/(?:^|:|,)(?:\s*\[)+/g,s=/(webkit)[ \/]([\w.]+)/,t=/(opera)(?:.*version)?[ \/]([\w.]+)/,u=/(msie) ([\w.]+)/,v=/(mozilla)(?:.*? rv:([\w.]+))?/,w=/-([a-z]|[0-9])/ig,x=/^-ms-/,y=function(a,b){return(b+"").toUpperCase()},z=d.userAgent,A,B,C,D=Object.prototype.toString,E=Object.prototype.hasOwnProperty,F=Array.prototype.push,G=Array.prototype.slice,H=String.prototype.trim,I=Array.prototype.indexOf,J={};e.fn=e.prototype={constructor:e,init:function(a,d,f){var g,h,j,k;if(!a)return this;if(a.nodeType){this.context=this[0]=a,this.length=1;return this}if(a==="body"&&!d&&c.body){this.context=c,this[0]=c.body,this.selector=a,this.length=1;return this}if(typeof a=="string"){a.charAt(0)!=="<"||a.charAt(a.length-1)!==">"||a.length<3?g=i.exec(a):g=[null,a,null];if(g&&(g[1]||!d)){if(g[1]){d=d instanceof e?d[0]:d,k=d?d.ownerDocument||d:c,j=n.exec(a),j?e.isPlainObject(d)?(a=[c.createElement(j[1])],e.fn.attr.call(a,d,!0)):a=[k.createElement(j[1])]:(j=e.buildFragment([g[1]],[k]),a=(j.cacheable?e.clone(j.fragment):j.fragment).childNodes);return e.merge(this,a)}h=c.getElementById(g[2]);if(h&&h.parentNode){if(h.id!==g[2])return f.find(a);this.length=1,this[0]=h}this.context=c,this.selector=a;return this}return!d||d.jquery?(d||f).find(a):this.constructor(d).find(a)}if(e.isFunction(a))return f.ready(a);a.selector!==b&&(this.selector=a.selector,this.context=a.context);return e.makeArray(a,this)},selector:"",jquery:"1.7",length:0,size:function(){return this.length},toArray:function(){return G.call(this,0)},get:function(a){return a==null?this.toArray():a<0?this[this.length+a]:this[a]},pushStack:function(a,b,c){var d=this.constructor();e.isArray(a)?F.apply(d,a):e.merge(d,a),d.prevObject=this,d.context=this.context,b==="find"?d.selector=this.selector+(this.selector?" ":"")+c:b&&(d.selector=this.selector+"."+b+"("+c+")");return d},each:function(a,b){return e.each(this,a,b)},ready:function(a){e.bindReady(),B.add(a);return this},eq:function(a){return a===-1?this.slice(a):this.slice(a,+a+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(G.apply(this,arguments),"slice",G.call(arguments).join(","))},map:function(a){return this.pushStack(e.map(this,function(b,c){return a.call(b,c,b)}))},end:function(){return this.prevObject||this.constructor(null)},push:F,sort:[].sort,splice:[].splice},e.fn.init.prototype=e.fn,e.extend=e.fn.extend=function(){var a,c,d,f,g,h,i=arguments[0]||{},j=1,k=arguments.length,l=!1;typeof i=="boolean"&&(l=i,i=arguments[1]||{},j=2),typeof i!="object"&&!e.isFunction(i)&&(i={}),k===j&&(i=this,--j);for(;j0)return;B.fireWith(c,[e]),e.fn.trigger&&e(c).trigger("ready").unbind("ready")}},bindReady:function(){if(!B){B=e.Callbacks("once memory");if(c.readyState==="complete")return setTimeout(e.ready,1);if(c.addEventListener)c.addEventListener("DOMContentLoaded",C,!1),a.addEventListener("load",e.ready,!1);else if(c.attachEvent){c.attachEvent("onreadystatechange",C),a.attachEvent("onload",e.ready);var b=!1;try{b=a.frameElement==null}catch(d){}c.documentElement.doScroll&&b&&K()}}},isFunction:function(a){return e.type(a)==="function"},isArray:Array.isArray||function(a){return e.type(a)==="array"},isWindow:function(a){return a&&typeof a=="object"&&"setInterval"in a},isNumeric:function(a){return a!=null&&m.test(a)&&!isNaN(a)},type:function(a){return a==null?String(a):J[D.call(a)]||"object"},isPlainObject:function(a){if(!a||e.type(a)!=="object"||a.nodeType||e.isWindow(a))return!1;try{if(a.constructor&&!E.call(a,"constructor")&&!E.call(a.constructor.prototype,"isPrototypeOf"))return!1}catch(c){return!1}var d;for(d in a);return d===b||E.call(a,d)},isEmptyObject:function(a){for(var b in a)return!1;return!0},error:function(a){throw a},parseJSON:function(b){if(typeof b!="string"||!b)return null;b=e.trim(b);if(a.JSON&&a.JSON.parse)return a.JSON.parse(b);if(o.test(b.replace(p,"@").replace(q,"]").replace(r,"")))return(new Function("return "+b))();e.error("Invalid JSON: "+b)},parseXML:function(c){var d,f;try{a.DOMParser?(f=new DOMParser,d=f.parseFromString(c,"text/xml")):(d=new ActiveXObject("Microsoft.XMLDOM"),d.async="false",d.loadXML(c))}catch(g){d=b}(!d||!d.documentElement||d.getElementsByTagName("parsererror").length)&&e.error("Invalid XML: "+c);return d},noop:function(){},globalEval:function(b){b&&j.test(b)&&(a.execScript||function(b){a.eval.call(a,b)})(b)},camelCase:function(a){return a.replace(x,"ms-").replace(w,y)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toUpperCase()===b.toUpperCase()},each:function(a,c,d){var f,g=0,h=a.length,i=h===b||e.isFunction(a);if(d){if(i){for(f in a)if(c.apply(a[f],d)===!1)break}else for(;g0&&a[0]&&a[j-1]||j===0||e.isArray(a));if(k)for(;i1?i.call(arguments,0):b,j.notifyWith(k,e)}}function l(a){return function(c){b[a]=arguments.length>1?i.call(arguments,0):c,--g||j.resolveWith(j,b)}}var b=i.call(arguments,0),c=0,d=b.length,e=Array(d),g=d,h=d,j=d<=1&&a&&f.isFunction(a.promise)?a:f.Deferred(),k=j.promise();if(d>1){for(;c
a",d=a.getElementsByTagName("*"),e=a.getElementsByTagName("a")[0];if(!d||!d.length||!e)return{};g=c.createElement("select"),h=g.appendChild(c.createElement("option")),i=a.getElementsByTagName("input")[0],k={leadingWhitespace:a.firstChild.nodeType===3,tbody:!a.getElementsByTagName("tbody").length,htmlSerialize:!!a.getElementsByTagName("link").length,style:/top/.test(e.getAttribute("style")),hrefNormalized:e.getAttribute("href")==="/a",opacity:/^0.55/.test(e.style.opacity),cssFloat:!!e.style.cssFloat,unknownElems:!!a.getElementsByTagName("nav").length,checkOn:i.value==="on",optSelected:h.selected,getSetAttribute:a.className!=="t",enctype:!!c.createElement("form").enctype,submitBubbles:!0,changeBubbles:!0,focusinBubbles:!1,deleteExpando:!0,noCloneEvent:!0,inlineBlockNeedsLayout:!1,shrinkWrapBlocks:!1,reliableMarginRight:!0},i.checked=!0,k.noCloneChecked=i.cloneNode(!0).checked,g.disabled=!0,k.optDisabled=!h.disabled;try{delete a.test}catch(v){k.deleteExpando=!1}!a.addEventListener&&a.attachEvent&&a.fireEvent&&(a.attachEvent("onclick",function(){k.noCloneEvent=!1}),a.cloneNode(!0).fireEvent("onclick")),i=c.createElement("input"),i.value="t",i.setAttribute("type","radio"),k.radioValue=i.value==="t",i.setAttribute("checked","checked"),a.appendChild(i),l=c.createDocumentFragment(),l.appendChild(a.lastChild),k.checkClone=l.cloneNode(!0).cloneNode(!0).lastChild.checked,a.innerHTML="",a.style.width=a.style.paddingLeft="1px",m=c.getElementsByTagName("body")[0],o=c.createElement(m?"div":"body"),p={visibility:"hidden",width:0,height:0,border:0,margin:0,background:"none"},m&&f.extend(p,{position:"absolute",left:"-999px",top:"-999px"});for(t in p)o.style[t]=p[t];o.appendChild(a),n=m||b,n.insertBefore(o,n.firstChild),k.appendChecked=i.checked,k.boxModel=a.offsetWidth===2,"zoom"in a.style&&(a.style.display="inline",a.style.zoom=1,k.inlineBlockNeedsLayout=a.offsetWidth===2,a.style.display="",a.innerHTML="
",k.shrinkWrapBlocks=a.offsetWidth!==2),a.innerHTML="
t
",q=a.getElementsByTagName("td"),u=q[0].offsetHeight===0,q[0].style.display="",q[1].style.display="none",k.reliableHiddenOffsets=u&&q[0].offsetHeight===0,a.innerHTML="",c.defaultView&&c.defaultView.getComputedStyle&&(j=c.createElement("div"),j.style.width="0",j.style.marginRight="0",a.appendChild(j),k.reliableMarginRight=(parseInt((c.defaultView.getComputedStyle(j,null)||{marginRight:0}).marginRight,10)||0)===0);if(a.attachEvent)for(t in{submit:1,change:1,focusin:1})s="on"+t,u=s in a,u||(a.setAttribute(s,"return;"),u=typeof a[s]=="function"),k[t+"Bubbles"]=u;f(function(){var a,b,d,e,g,h,i=1,j="position:absolute;top:0;left:0;width:1px;height:1px;margin:0;",l="visibility:hidden;border:0;",n="style='"+j+"border:5px solid #000;padding:0;'",p="
"+""+"
";m=c.getElementsByTagName("body")[0];!m||(a=c.createElement("div"),a.style.cssText=l+"width:0;height:0;position:static;top:0;margin-top:"+i+"px",m.insertBefore(a,m.firstChild),o=c.createElement("div"),o.style.cssText=j+l,o.innerHTML=p,a.appendChild(o),b=o.firstChild,d=b.firstChild,g=b.nextSibling.firstChild.firstChild,h={doesNotAddBorder:d.offsetTop!==5,doesAddBorderForTableAndCells:g.offsetTop===5},d.style.position="fixed",d.style.top="20px",h.fixedPosition=d.offsetTop===20||d.offsetTop===15,d.style.position=d.style.top="",b.style.overflow="hidden",b.style.position="relative",h.subtractsBorderForOverflowNotVisible=d.offsetTop===-5,h.doesNotIncludeMarginInBodyOffset=m.offsetTop!==i,m.removeChild(a),o=a=null,f.extend(k,h))}),o.innerHTML="",n.removeChild(o),o=l=g=h=m=j=a=i=null;return k}(),f.boxModel=f.support.boxModel;var j=/^(?:\{.*\}|\[.*\])$/,k=/([A-Z])/g;f.extend({cache:{},uuid:0,expando:"jQuery"+(f.fn.jquery+Math.random()).replace(/\D/g,""),noData:{embed:!0,object:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",applet:!0},hasData:function(a){a=a.nodeType?f.cache[a[f.expando]]:a[f.expando];return!!a&&!m(a)},data:function(a,c,d,e){if(!!f.acceptData(a)){var g,h,i,j=f.expando,k=typeof c=="string",l=a.nodeType,m=l?f.cache:a,n=l?a[f.expando]:a[f.expando]&&f.expando,o=c==="events";if((!n||!m[n]||!o&&!e&&!m[n].data)&&k&&d===b)return;n||(l?a[f.expando]=n=++f.uuid:n=f.expando),m[n]||(m[n]={},l||(m[n].toJSON=f.noop));if(typeof c=="object"||typeof c=="function")e?m[n]=f.extend(m[n],c):m[n].data=f.extend(m[n].data,c);g=h=m[n],e||(h.data||(h.data={}),h=h.data),d!==b&&(h[f.camelCase(c)]=d);if(o&&!h[c])return g.events;k?(i=h[c],i==null&&(i=h[f.camelCase(c)])):i=h;return i}},removeData:function(a,b,c){if(!!f.acceptData(a)){var d,e,g,h=f.expando,i=a.nodeType,j=i?f.cache:a,k=i?a[f.expando]:f.expando;if(!j[k])return;if(b){d=c?j[k]:j[k].data;if(d){f.isArray(b)?b=b:b in d?b=[b]:(b=f.camelCase(b),b in d?b=[b]:b=b.split(" "));for(e=0,g=b.length;e-1)return!0;return!1},val:function(a){var c,d,e,g=this[0];if(!arguments.length){if(g){c=f.valHooks[g.nodeName.toLowerCase()]||f.valHooks[g.type];if(c&&"get"in c&&(d=c.get(g,"value"))!==b)return d;d=g.value;return typeof d=="string"?d.replace(q,""):d==null?"":d}return b}e=f.isFunction(a);return this.each(function(d){var g=f(this),h;if(this.nodeType===1){e?h=a.call(this,d,g.val()):h=a,h==null?h="":typeof h=="number"?h+="":f.isArray(h)&&(h=f.map(h,function(a){return a==null?"":a+""})),c=f.valHooks[this.nodeName.toLowerCase()]||f.valHooks[this.type];if(!c||!("set"in c)||c.set(this,h,"value")===b)this.value=h}})}}),f.extend({valHooks:{option:{get:function(a){var b=a.attributes.value;return!b||b.specified?a.value:a.text}},select:{get:function(a){var b,c,d,e,g=a.selectedIndex,h=[],i=a.options,j=a.type==="select-one";if(g<0)return null;c=j?g:0,d=j?g+1:i.length;for(;c=0}),c.length||(a.selectedIndex=-1);return c}}},attrFn:{val:!0,css:!0,html:!0,text:!0,data:!0,width:!0,height:!0,offset:!0},attr:function(a,c,d,e){var g,h,i,j=a.nodeType;if(!a||j===3||j===8||j===2)return b;if(e&&c in f.attrFn)return f(a)[c](d);if(!("getAttribute"in a))return f.prop(a,c,d);i=j!==1||!f.isXMLDoc(a),i&&(c=c.toLowerCase(),h=f.attrHooks[c]||(u.test(c)?x:w));if(d!==b){if(d===null){f.removeAttr(a,c);return b}if(h&&"set"in h&&i&&(g=h.set(a,d,c))!==b)return g;a.setAttribute(c,""+d);return d}if(h&&"get"in h&&i&&(g=h.get(a,c))!==null)return g;g=a.getAttribute(c);return g===null?b:g},removeAttr:function(a,b){var c,d,e,g,h=0;if(a.nodeType===1){d=(b||"").split(p),g=d.length;for(;h=0}})});var z=/\.(.*)$/,A=/^(?:textarea|input|select)$/i,B=/\./g,C=/ /g,D=/[^\w\s.|`]/g,E=/^([^\.]*)?(?:\.(.+))?$/,F=/\bhover(\.\S+)?/,G=/^key/,H=/^(?:mouse|contextmenu)|click/,I=/^(\w*)(?:#([\w\-]+))?(?:\.([\w\-]+))?$/,J=function(a){var b=I.exec(a);b&& 3 | (b[1]=(b[1]||"").toLowerCase(),b[3]=b[3]&&new RegExp("(?:^|\\s)"+b[3]+"(?:\\s|$)"));return b},K=function(a,b){return(!b[1]||a.nodeName.toLowerCase()===b[1])&&(!b[2]||a.id===b[2])&&(!b[3]||b[3].test(a.className))},L=function(a){return f.event.special.hover?a:a.replace(F,"mouseenter$1 mouseleave$1")};f.event={add:function(a,c,d,e,g){var h,i,j,k,l,m,n,o,p,q,r,s;if(!(a.nodeType===3||a.nodeType===8||!c||!d||!(h=f._data(a)))){d.handler&&(p=d,d=p.handler),d.guid||(d.guid=f.guid++),j=h.events,j||(h.events=j={}),i=h.handle,i||(h.handle=i=function(a){return typeof f!="undefined"&&(!a||f.event.triggered!==a.type)?f.event.dispatch.apply(i.elem,arguments):b},i.elem=a),c=L(c).split(" ");for(k=0;k=0&&(h=h.slice(0,-1),k=!0),h.indexOf(".")>=0&&(i=h.split("."),h=i.shift(),i.sort());if((!e||f.event.customEvent[h])&&!f.event.global[h])return;c=typeof c=="object"?c[f.expando]?c:new f.Event(h,c):new f.Event(h),c.type=h,c.isTrigger=!0,c.exclusive=k,c.namespace=i.join("."),c.namespace_re=c.namespace?new RegExp("(^|\\.)"+i.join("\\.(?:.*\\.)?")+"(\\.|$)"):null,o=h.indexOf(":")<0?"on"+h:"",(g||!e)&&c.preventDefault();if(!e){j=f.cache;for(l in j)j[l].events&&j[l].events[h]&&f.event.trigger(c,d,j[l].handle.elem,!0);return}c.result=b,c.target||(c.target=e),d=d!=null?f.makeArray(d):[],d.unshift(c),p=f.event.special[h]||{};if(p.trigger&&p.trigger.apply(e,d)===!1)return;r=[[e,p.bindType||h]];if(!g&&!p.noBubble&&!f.isWindow(e)){s=p.delegateType||h,n=null;for(m=e.parentNode;m;m=m.parentNode)r.push([m,s]),n=m;n&&n===e.ownerDocument&&r.push([n.defaultView||n.parentWindow||a,s])}for(l=0;l=0:t===b&&(t=o[s]=r.quick?K(m,r.quick):f(m).is(s)),t&&q.push(r);q.length&&j.push({elem:m,matches:q})}d.length>e&&j.push({elem:this,matches:d.slice(e)});for(k=0;k0?this.bind(b,a,c):this.trigger(b)},f.attrFn&&(f.attrFn[b]=!0),G.test(b)&&(f.event.fixHooks[b]=f.event.keyHooks),H.test(b)&&(f.event.fixHooks[b]=f.event.mouseHooks)}),function(){function x(a,b,c,e,f,g){for(var h=0,i=e.length;h0){k=j;break}}j=j[a]}e[h]=k}}}function w(a,b,c,e,f,g){for(var h=0,i=e.length;h+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,d="sizcache"+(Math.random()+"").replace(".",""),e=0,g=Object.prototype.toString,h=!1,i=!0,j=/\\/g,k=/\r\n/g,l=/\W/;[0,0].sort(function(){i=!1;return 0});var m=function(b,d,e,f){e=e||[],d=d||c;var h=d;if(d.nodeType!==1&&d.nodeType!==9)return[];if(!b||typeof b!="string")return e;var i,j,k,l,n,q,r,t,u=!0,v=m.isXML(d),w=[],x=b;do{a.exec(""),i=a.exec(x);if(i){x=i[3],w.push(i[1]);if(i[2]){l=i[3];break}}}while(i);if(w.length>1&&p.exec(b))if(w.length===2&&o.relative[w[0]])j=y(w[0]+w[1],d,f);else{j=o.relative[w[0]]?[d]:m(w.shift(),d);while(w.length)b=w.shift(),o.relative[b]&&(b+=w.shift()),j=y(b,j,f)}else{!f&&w.length>1&&d.nodeType===9&&!v&&o.match.ID.test(w[0])&&!o.match.ID.test(w[w.length-1])&&(n=m.find(w.shift(),d,v),d=n.expr?m.filter(n.expr,n.set)[0]:n.set[0]);if(d){n=f?{expr:w.pop(),set:s(f)}:m.find(w.pop(),w.length===1&&(w[0]==="~"||w[0]==="+")&&d.parentNode?d.parentNode:d,v),j=n.expr?m.filter(n.expr,n.set):n.set,w.length>0?k=s(j):u=!1;while(w.length)q=w.pop(),r=q,o.relative[q]?r=w.pop():q="",r==null&&(r=d),o.relative[q](k,r,v)}else k=w=[]}k||(k=j),k||m.error(q||b);if(g.call(k)==="[object Array]")if(!u)e.push.apply(e,k);else if(d&&d.nodeType===1)for(t=0;k[t]!=null;t++)k[t]&&(k[t]===!0||k[t].nodeType===1&&m.contains(d,k[t]))&&e.push(j[t]);else for(t=0;k[t]!=null;t++)k[t]&&k[t].nodeType===1&&e.push(j[t]);else s(k,e);l&&(m(l,h,e,f),m.uniqueSort(e));return e};m.uniqueSort=function(a){if(u){h=i,a.sort(u);if(h)for(var b=1;b0},m.find=function(a,b,c){var d,e,f,g,h,i;if(!a)return[];for(e=0,f=o.order.length;e":function(a,b){var c,d=typeof b=="string",e=0,f=a.length;if(d&&!l.test(b)){b=b.toLowerCase();for(;e=0)?c||d.push(h):c&&(b[g]=!1));return!1},ID:function(a){return a[1].replace(j,"")},TAG:function(a,b){return a[1].replace(j,"").toLowerCase()},CHILD:function(a){if(a[1]==="nth"){a[2]||m.error(a[0]),a[2]=a[2].replace(/^\+|\s*/g,"");var b=/(-?)(\d*)(?:n([+\-]?\d*))?/.exec(a[2]==="even"&&"2n"||a[2]==="odd"&&"2n+1"||!/\D/.test(a[2])&&"0n+"+a[2]||a[2]);a[2]=b[1]+(b[2]||1)-0,a[3]=b[3]-0}else a[2]&&m.error(a[0]);a[0]=e++;return a},ATTR:function(a,b,c,d,e,f){var g=a[1]=a[1].replace(j,"");!f&&o.attrMap[g]&&(a[1]=o.attrMap[g]),a[4]=(a[4]||a[5]||"").replace(j,""),a[2]==="~="&&(a[4]=" "+a[4]+" ");return a},PSEUDO:function(b,c,d,e,f){if(b[1]==="not")if((a.exec(b[3])||"").length>1||/^\w/.test(b[3]))b[3]=m(b[3],null,null,c);else{var g=m.filter(b[3],c,d,!0^f);d||e.push.apply(e,g);return!1}else if(o.match.POS.test(b[0])||o.match.CHILD.test(b[0]))return!0;return b},POS:function(a){a.unshift(!0);return a}},filters:{enabled:function(a){return a.disabled===!1&&a.type!=="hidden"},disabled:function(a){return a.disabled===!0},checked:function(a){return a.checked===!0},selected:function(a){a.parentNode&&a.parentNode.selectedIndex;return a.selected===!0},parent:function(a){return!!a.firstChild},empty:function(a){return!a.firstChild},has:function(a,b,c){return!!m(c[3],a).length},header:function(a){return/h\d/i.test(a.nodeName)},text:function(a){var b=a.getAttribute("type"),c=a.type;return a.nodeName.toLowerCase()==="input"&&"text"===c&&(b===c||b===null)},radio:function(a){return a.nodeName.toLowerCase()==="input"&&"radio"===a.type},checkbox:function(a){return a.nodeName.toLowerCase()==="input"&&"checkbox"===a.type},file:function(a){return a.nodeName.toLowerCase()==="input"&&"file"===a.type},password:function(a){return a.nodeName.toLowerCase()==="input"&&"password"===a.type},submit:function(a){var b=a.nodeName.toLowerCase();return(b==="input"||b==="button")&&"submit"===a.type},image:function(a){return a.nodeName.toLowerCase()==="input"&&"image"===a.type},reset:function(a){var b=a.nodeName.toLowerCase();return(b==="input"||b==="button")&&"reset"===a.type},button:function(a){var b=a.nodeName.toLowerCase();return b==="input"&&"button"===a.type||b==="button"},input:function(a){return/input|select|textarea|button/i.test(a.nodeName)},focus:function(a){return a===a.ownerDocument.activeElement}},setFilters:{first:function(a,b){return b===0},last:function(a,b,c,d){return b===d.length-1},even:function(a,b){return b%2===0},odd:function(a,b){return b%2===1},lt:function(a,b,c){return bc[3]-0},nth:function(a,b,c){return c[3]-0===b},eq:function(a,b,c){return c[3]-0===b}},filter:{PSEUDO:function(a,b,c,d){var e=b[1],f=o.filters[e];if(f)return f(a,c,b,d);if(e==="contains")return(a.textContent||a.innerText||n([a])||"").indexOf(b[3])>=0;if(e==="not"){var g=b[3];for(var h=0,i=g.length;h=0}},ID:function(a,b){return a.nodeType===1&&a.getAttribute("id")===b},TAG:function(a,b){return b==="*"&&a.nodeType===1||!!a.nodeName&&a.nodeName.toLowerCase()===b},CLASS:function(a,b){return(" "+(a.className||a.getAttribute("class"))+" ").indexOf(b)>-1},ATTR:function(a,b){var c=b[1],d=m.attr?m.attr(a,c):o.attrHandle[c]?o.attrHandle[c](a):a[c]!=null?a[c]:a.getAttribute(c),e=d+"",f=b[2],g=b[4];return d==null?f==="!=":!f&&m.attr?d!=null:f==="="?e===g:f==="*="?e.indexOf(g)>=0:f==="~="?(" "+e+" ").indexOf(g)>=0:g?f==="!="?e!==g:f==="^="?e.indexOf(g)===0:f==="$="?e.substr(e.length-g.length)===g:f==="|="?e===g||e.substr(0,g.length+1)===g+"-":!1:e&&d!==!1},POS:function(a,b,c,d){var e=b[2],f=o.setFilters[e];if(f)return f(a,c,b,d)}}},p=o.match.POS,q=function(a,b){return"\\"+(b-0+1)};for(var r in o.match)o.match[r]=new RegExp(o.match[r].source+/(?![^\[]*\])(?![^\(]*\))/.source),o.leftMatch[r]=new RegExp(/(^(?:.|\r|\n)*?)/.source+o.match[r].source.replace(/\\(\d+)/g,q));var s=function(a,b){a=Array.prototype.slice.call(a,0);if(b){b.push.apply(b,a);return b}return a};try{Array.prototype.slice.call(c.documentElement.childNodes,0)[0].nodeType}catch(t){s=function(a,b){var c=0,d=b||[];if(g.call(a)==="[object Array]")Array.prototype.push.apply(d,a);else if(typeof a.length=="number")for(var e=a.length;c",e.insertBefore(a,e.firstChild),c.getElementById(d)&&(o.find.ID=function(a,c,d){if(typeof c.getElementById!="undefined"&&!d){var e=c.getElementById(a[1]);return e?e.id===a[1]||typeof e.getAttributeNode!="undefined"&&e.getAttributeNode("id").nodeValue===a[1]?[e]:b:[]}},o.filter.ID=function(a,b){var c=typeof a.getAttributeNode!="undefined"&&a.getAttributeNode("id");return a.nodeType===1&&c&&c.nodeValue===b}),e.removeChild(a),e=a=null}(),function(){var a=c.createElement("div");a.appendChild(c.createComment("")),a.getElementsByTagName("*").length>0&&(o.find.TAG=function(a,b){var c=b.getElementsByTagName(a[1]);if(a[1]==="*"){var d=[];for(var e=0;c[e];e++)c[e].nodeType===1&&d.push(c[e]);c=d}return c}),a.innerHTML="",a.firstChild&&typeof a.firstChild.getAttribute!="undefined"&&a.firstChild.getAttribute("href")!=="#"&&(o.attrHandle.href=function(a){return a.getAttribute("href",2)}),a=null}(),c.querySelectorAll&&function(){var a=m,b=c.createElement("div"),d="__sizzle__";b.innerHTML="

";if(!b.querySelectorAll||b.querySelectorAll(".TEST").length!==0){m=function(b,e,f,g){e=e||c;if(!g&&!m.isXML(e)){var h=/^(\w+$)|^\.([\w\-]+$)|^#([\w\-]+$)/.exec(b);if(h&&(e.nodeType===1||e.nodeType===9)){if(h[1])return s(e.getElementsByTagName(b),f);if(h[2]&&o.find.CLASS&&e.getElementsByClassName)return s(e.getElementsByClassName(h[2]),f)}if(e.nodeType===9){if(b==="body"&&e.body)return s([e.body],f);if(h&&h[3]){var i=e.getElementById(h[3]);if(!i||!i.parentNode)return s([],f);if(i.id===h[3])return s([i],f)}try{return s(e.querySelectorAll(b),f)}catch(j){}}else if(e.nodeType===1&&e.nodeName.toLowerCase()!=="object"){var k=e,l=e.getAttribute("id"),n=l||d,p=e.parentNode,q=/^\s*[+~]/.test(b);l?n=n.replace(/'/g,"\\$&"):e.setAttribute("id",n),q&&p&&(e=e.parentNode);try{if(!q||p)return s(e.querySelectorAll("[id='"+n+"'] "+b),f)}catch(r){}finally{l||k.removeAttribute("id")}}}return a(b,e,f,g)};for(var e in a)m[e]=a[e];b=null}}(),function(){var a=c.documentElement,b=a.matchesSelector||a.mozMatchesSelector||a.webkitMatchesSelector||a.msMatchesSelector;if(b){var d=!b.call(c.createElement("div"),"div"),e=!1;try{b.call(c.documentElement,"[test!='']:sizzle")}catch(f){e=!0}m.matchesSelector=function(a,c){c=c.replace(/\=\s*([^'"\]]*)\s*\]/g,"='$1']");if(!m.isXML(a))try{if(e||!o.match.PSEUDO.test(c)&&!/!=/.test(c)){var f=b.call(a,c);if(f||!d||a.document&&a.document.nodeType!==11)return f}}catch(g){}return m(c,null,null,[a]).length>0}}}(),function(){var a=c.createElement("div");a.innerHTML="
";if(!!a.getElementsByClassName&&a.getElementsByClassName("e").length!==0){a.lastChild.className="e";if(a.getElementsByClassName("e").length===1)return;o.order.splice(1,0,"CLASS"),o.find.CLASS=function(a,b,c){if(typeof b.getElementsByClassName!="undefined"&&!c)return b.getElementsByClassName(a[1])},a=null}}(),c.documentElement.contains?m.contains=function(a,b){return a!==b&&(a.contains?a.contains(b):!0)}:c.documentElement.compareDocumentPosition?m.contains=function(a,b){return!!(a.compareDocumentPosition(b)&16)}:m.contains=function(){return!1},m.isXML=function(a){var b=(a?a.ownerDocument||a:0).documentElement;return b?b.nodeName!=="HTML":!1};var y=function(a,b,c){var d,e=[],f="",g=b.nodeType?[b]:b;while(d=o.match.PSEUDO.exec(a))f+=d[0],a=a.replace(o.match.PSEUDO,"");a=o.relative[a]?a+"*":a;for(var h=0,i=g.length;h0)for(h=g;h=0:f.filter(a,this).length>0:this.filter(a).length>0)},closest:function(a,b){var c=[],d,e,g=this[0];if(f.isArray(a)){var h=1;while(g&&g.ownerDocument&&g!==b){for(d=0;d-1:f.find.matchesSelector(g,a)){c.push(g);break}g=g.parentNode;if(!g||!g.ownerDocument||g===b||g.nodeType===11)break}}c=c.length>1?f.unique(c):c;return this.pushStack(c,"closest",a)},index:function(a){if(!a)return this[0]&&this[0].parentNode?this.prevAll().length:-1;if(typeof a=="string")return f.inArray(this[0],f(a));return f.inArray(a.jquery?a[0]:a,this)},add:function(a,b){var c=typeof a=="string"?f(a,b):f.makeArray(a&&a.nodeType?[a]:a),d=f.merge(this.get(),c);return this.pushStack(V(c[0])||V(d[0])?d:f.unique(d))},andSelf:function(){return this.add(this.prevObject)}}),f.each({parent:function(a){var b=a.parentNode;return b&&b.nodeType!==11?b:null},parents:function(a){return f.dir(a,"parentNode")},parentsUntil:function(a,b,c){return f.dir(a,"parentNode",c)},next:function(a){return f.nth(a,2,"nextSibling")},prev:function(a){return f.nth(a,2,"previousSibling")},nextAll:function(a){return f.dir(a,"nextSibling")},prevAll:function(a){return f.dir(a,"previousSibling")},nextUntil:function(a,b,c){return f.dir(a,"nextSibling",c)},prevUntil:function(a,b,c){return f.dir(a,"previousSibling",c)},siblings:function(a){return f.sibling(a.parentNode.firstChild,a)},children:function(a){return f.sibling(a.firstChild)},contents:function(a){return f.nodeName(a,"iframe")?a.contentDocument||a.contentWindow.document:f.makeArray(a.childNodes)}},function(a,b){f.fn[a]=function(c,d){var e=f.map(this,b,c),g=S.call(arguments);O.test(a)||(d=c),d&&typeof d=="string"&&(e=f.filter(d,e)),e=this.length>1&&!U[a]?f.unique(e):e,(this.length>1||Q.test(d))&&P.test(a)&&(e=e.reverse());return this.pushStack(e,a,g.join(","))}}),f.extend({filter:function(a,b,c){c&&(a=":not("+a+")");return b.length===1?f.find.matchesSelector(b[0],a)?[b[0]]:[]:f.find.matches(a,b)},dir:function(a,c,d){var e=[],g=a[c];while(g&&g.nodeType!==9&&(d===b||g.nodeType!==1||!f(g).is(d)))g.nodeType===1&&e.push(g),g=g[c];return e},nth:function(a,b,c,d){b=b||1;var e=0;for(;a;a=a[c])if(a.nodeType===1&&++e===b)break;return a},sibling:function(a,b){var c=[];for(;a;a=a.nextSibling)a.nodeType===1&&a!==b&&c.push(a);return c}});var Y="abbr article aside audio canvas datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video",Z=/ jQuery\d+="(?:\d+|null)"/g,$=/^\s+/,_=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,ba=/<([\w:]+)/,bb=/",""],legend:[1,"
","
"],thead:[1,"","
"],tr:[2,"","
"],td:[3,"","
"],col:[2,"","
"],area:[1,"",""],_default:[0,"",""]},bk=X(c);bj.optgroup=bj.option,bj.tbody=bj.tfoot=bj.colgroup=bj.caption=bj.thead,bj.th=bj.td,f.support.htmlSerialize||(bj._default=[1,"div
","
"]),f.fn.extend({text:function(a){if(f.isFunction(a))return this.each(function(b){var c=f(this);c.text(a.call(this,b,c.text()))});if(typeof a!="object"&&a!==b)return this.empty().append((this[0]&&this[0].ownerDocument||c).createTextNode(a));return f.text(this)},wrapAll:function(a){if(f.isFunction(a))return this.each(function(b){f(this).wrapAll(a.call(this,b))});if(this[0]){var b=f(a,this[0].ownerDocument).eq(0).clone(!0);this[0].parentNode&&b.insertBefore(this[0]),b.map(function(){var a=this;while(a.firstChild&&a.firstChild.nodeType===1)a=a.firstChild;return a}).append(this)}return this},wrapInner:function(a){if(f.isFunction(a))return this.each(function(b){f(this).wrapInner(a.call(this,b))});return this.each(function(){var b=f(this),c=b.contents();c.length?c.wrapAll(a):b.append(a)})},wrap:function(a){return this.each(function(){f(this).wrapAll(a)})},unwrap:function(){return this.parent().each(function(){f.nodeName(this,"body")||f(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.appendChild(a)})},prepend:function(){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.insertBefore(a,this.firstChild)})},before:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this)});if(arguments.length){var a=f(arguments[0]);a.push.apply(a,this.toArray());return this.pushStack(a,"before",arguments)}},after:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this.nextSibling)});if(arguments.length){var a=this.pushStack(this,"after" 4 | ,arguments);a.push.apply(a,f(arguments[0]).toArray());return a}},remove:function(a,b){for(var c=0,d;(d=this[c])!=null;c++)if(!a||f.filter(a,[d]).length)!b&&d.nodeType===1&&(f.cleanData(d.getElementsByTagName("*")),f.cleanData([d])),d.parentNode&&d.parentNode.removeChild(d);return this},empty:function(){for(var a=0,b;(b=this[a])!=null;a++){b.nodeType===1&&f.cleanData(b.getElementsByTagName("*"));while(b.firstChild)b.removeChild(b.firstChild)}return this},clone:function(a,b){a=a==null?!1:a,b=b==null?a:b;return this.map(function(){return f.clone(this,a,b)})},html:function(a){if(a===b)return this[0]&&this[0].nodeType===1?this[0].innerHTML.replace(Z,""):null;if(typeof a=="string"&&!bd.test(a)&&(f.support.leadingWhitespace||!$.test(a))&&!bj[(ba.exec(a)||["",""])[1].toLowerCase()]){a=a.replace(_,"<$1>");try{for(var c=0,d=this.length;c1&&l0?this.clone(!0):this).get();f(e[h])[b](j),d=d.concat(j)}return this.pushStack(d,a,e.selector)}}),f.extend({clone:function(a,b,c){var d=a.cloneNode(!0),e,g,h;if((!f.support.noCloneEvent||!f.support.noCloneChecked)&&(a.nodeType===1||a.nodeType===11)&&!f.isXMLDoc(a)){bn(a,d),e=bo(a),g=bo(d);for(h=0;e[h];++h)g[h]&&bn(e[h],g[h])}if(b){bm(a,d);if(c){e=bo(a),g=bo(d);for(h=0;e[h];++h)bm(e[h],g[h])}}e=g=null;return d},clean:function(a,b,d,e){var g;b=b||c,typeof b.createElement=="undefined"&&(b=b.ownerDocument||b[0]&&b[0].ownerDocument||c);var h=[],i;for(var j=0,k;(k=a[j])!=null;j++){typeof k=="number"&&(k+="");if(!k)continue;if(typeof k=="string")if(!bc.test(k))k=b.createTextNode(k);else{k=k.replace(_,"<$1>");var l=(ba.exec(k)||["",""])[1].toLowerCase(),m=bj[l]||bj._default,n=m[0],o=b.createElement("div");b===c?bk.appendChild(o):X(b).appendChild(o),o.innerHTML=m[1]+k+m[2];while(n--)o=o.lastChild;if(!f.support.tbody){var p=bb.test(k),q=l==="table"&&!p?o.firstChild&&o.firstChild.childNodes:m[1]===""&&!p?o.childNodes:[];for(i=q.length-1;i>=0;--i)f.nodeName(q[i],"tbody")&&!q[i].childNodes.length&&q[i].parentNode.removeChild(q[i])}!f.support.leadingWhitespace&&$.test(k)&&o.insertBefore(b.createTextNode($.exec(k)[0]),o.firstChild),k=o.childNodes}var r;if(!f.support.appendChecked)if(k[0]&&typeof (r=k.length)=="number")for(i=0;i=0)return b+"px"}}}),f.support.opacity||(f.cssHooks.opacity={get:function(a,b){return bt.test((b&&a.currentStyle?a.currentStyle.filter:a.style.filter)||"")?parseFloat(RegExp.$1)/100+"":b?"1":""},set:function(a,b){var c=a.style,d=a.currentStyle,e=f.isNumeric(b)?"alpha(opacity="+b*100+")":"",g=d&&d.filter||c.filter||"";c.zoom=1;if(b>=1&&f.trim(g.replace(bs,""))===""){c.removeAttribute("filter");if(d&&!d.filter)return}c.filter=bs.test(g)?g.replace(bs,e):g+" "+e}}),f(function(){f.support.reliableMarginRight||(f.cssHooks.marginRight={get:function(a,b){var c;f.swap(a,{display:"inline-block"},function(){b?c=bB(a,"margin-right","marginRight"):c=a.style.marginRight});return c}})}),c.defaultView&&c.defaultView.getComputedStyle&&(bC=function(a,c){var d,e,g;c=c.replace(bu,"-$1").toLowerCase();if(!(e=a.ownerDocument.defaultView))return b;if(g=e.getComputedStyle(a,null))d=g.getPropertyValue(c),d===""&&!f.contains(a.ownerDocument.documentElement,a)&&(d=f.style(a,c));return d}),c.documentElement.currentStyle&&(bD=function(a,b){var c,d,e,f=a.currentStyle&&a.currentStyle[b],g=a.style;f===null&&g&&(e=g[b])&&(f=e),!bv.test(f)&&bw.test(f)&&(c=g.left,d=a.runtimeStyle&&a.runtimeStyle.left,d&&(a.runtimeStyle.left=a.currentStyle.left),g.left=b==="fontSize"?"1em":f||0,f=g.pixelLeft+"px",g.left=c,d&&(a.runtimeStyle.left=d));return f===""?"auto":f}),bB=bC||bD,f.expr&&f.expr.filters&&(f.expr.filters.hidden=function(a){var b=a.offsetWidth,c=a.offsetHeight;return b===0&&c===0||!f.support.reliableHiddenOffsets&&(a.style&&a.style.display||f.css(a,"display"))==="none"},f.expr.filters.visible=function(a){return!f.expr.filters.hidden(a)});var bF=/%20/g,bG=/\[\]$/,bH=/\r?\n/g,bI=/#.*$/,bJ=/^(.*?):[ \t]*([^\r\n]*)\r?$/mg,bK=/^(?:color|date|datetime|datetime-local|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,bL=/^(?:about|app|app\-storage|.+\-extension|file|res|widget):$/,bM=/^(?:GET|HEAD)$/,bN=/^\/\//,bO=/\?/,bP=/)<[^<]*)*<\/script>/gi,bQ=/^(?:select|textarea)/i,bR=/\s+/,bS=/([?&])_=[^&]*/,bT=/^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/,bU=f.fn.load,bV={},bW={},bX,bY,bZ=["*/"]+["*"];try{bX=e.href}catch(b$){bX=c.createElement("a"),bX.href="",bX=bX.href}bY=bT.exec(bX.toLowerCase())||[],f.fn.extend({load:function(a,c,d){if(typeof a!="string"&&bU)return bU.apply(this,arguments);if(!this.length)return this;var e=a.indexOf(" ");if(e>=0){var g=a.slice(e,a.length);a=a.slice(0,e)}var h="GET";c&&(f.isFunction(c)?(d=c,c=b):typeof c=="object"&&(c=f.param(c,f.ajaxSettings.traditional),h="POST"));var i=this;f.ajax({url:a,type:h,dataType:"html",data:c,complete:function(a,b,c){c=a.responseText,a.isResolved()&&(a.done(function(a){c=a}),i.html(g?f("
").append(c.replace(bP,"")).find(g):c)),d&&i.each(d,[c,b,a])}});return this},serialize:function(){return f.param(this.serializeArray())},serializeArray:function(){return this.map(function(){return this.elements?f.makeArray(this.elements):this}).filter(function(){return this.name&&!this.disabled&&(this.checked||bQ.test(this.nodeName)||bK.test(this.type))}).map(function(a,b){var c=f(this).val();return c==null?null:f.isArray(c)?f.map(c,function(a,c){return{name:b.name,value:a.replace(bH,"\r\n")}}):{name:b.name,value:c.replace(bH,"\r\n")}}).get()}}),f.each("ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".split(" "),function(a,b){f.fn[b]=function(a){return this.bind(b,a)}}),f.each(["get","post"],function(a,c){f[c]=function(a,d,e,g){f.isFunction(d)&&(g=g||e,e=d,d=b);return f.ajax({type:c,url:a,data:d,success:e,dataType:g})}}),f.extend({getScript:function(a,c){return f.get(a,b,c,"script")},getJSON:function(a,b,c){return f.get(a,b,c,"json")},ajaxSetup:function(a,b){b?cb(a,f.ajaxSettings):(b=a,a=f.ajaxSettings),cb(a,b);return a},ajaxSettings:{url:bX,isLocal:bL.test(bY[1]),global:!0,type:"GET",contentType:"application/x-www-form-urlencoded",processData:!0,async:!0,accepts:{xml:"application/xml, text/xml",html:"text/html",text:"text/plain",json:"application/json, text/javascript","*":bZ},contents:{xml:/xml/,html:/html/,json:/json/},responseFields:{xml:"responseXML",text:"responseText"},converters:{"* text":a.String,"text html":!0,"text json":f.parseJSON,"text xml":f.parseXML},flatOptions:{context:!0,url:!0}},ajaxPrefilter:b_(bV),ajaxTransport:b_(bW),ajax:function(a,c){function w(a,c,l,m){if(s!==2){s=2,q&&clearTimeout(q),p=b,n=m||"",v.readyState=a>0?4:0;var o,r,u,w=c,x=l?cd(d,v,l):b,y,z;if(a>=200&&a<300||a===304){if(d.ifModified){if(y=v.getResponseHeader("Last-Modified"))f.lastModified[k]=y;if(z=v.getResponseHeader("Etag"))f.etag[k]=z}if(a===304)w="notmodified",o=!0;else try{r=ce(d,x),w="success",o=!0}catch(A){w="parsererror",u=A}}else{u=w;if(!w||a)w="error",a<0&&(a=0)}v.status=a,v.statusText=""+(c||w),o?h.resolveWith(e,[r,w,v]):h.rejectWith(e,[v,w,u]),v.statusCode(j),j=b,t&&g.trigger("ajax"+(o?"Success":"Error"),[v,d,o?r:u]),i.fireWith(e,[v,w]),t&&(g.trigger("ajaxComplete",[v,d]),--f.active||f.event.trigger("ajaxStop"))}}typeof a=="object"&&(c=a,a=b),c=c||{};var d=f.ajaxSetup({},c),e=d.context||d,g=e!==d&&(e.nodeType||e instanceof f)?f(e):f.event,h=f.Deferred(),i=f.Callbacks("once memory"),j=d.statusCode||{},k,l={},m={},n,o,p,q,r,s=0,t,u,v={readyState:0,setRequestHeader:function(a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this},getAllResponseHeaders:function(){return s===2?n:null},getResponseHeader:function(a){var c;if(s===2){if(!o){o={};while(c=bJ.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c},overrideMimeType:function(a){s||(d.mimeType=a);return this},abort:function(a){a=a||"abort",p&&p.abort(a),w(0,a);return this}};h.promise(v),v.success=v.done,v.error=v.fail,v.complete=i.add,v.statusCode=function(a){if(a){var b;if(s<2)for(b in a)j[b]=[j[b],a[b]];else b=a[v.status],v.then(b,b)}return this},d.url=((a||d.url)+"").replace(bI,"").replace(bN,bY[1]+"//"),d.dataTypes=f.trim(d.dataType||"*").toLowerCase().split(bR),d.crossDomain==null&&(r=bT.exec(d.url.toLowerCase()),d.crossDomain=!(!r||r[1]==bY[1]&&r[2]==bY[2]&&(r[3]||(r[1]==="http:"?80:443))==(bY[3]||(bY[1]==="http:"?80:443)))),d.data&&d.processData&&typeof d.data!="string"&&(d.data=f.param(d.data,d.traditional)),ca(bV,d,c,v);if(s===2)return!1;t=d.global,d.type=d.type.toUpperCase(),d.hasContent=!bM.test(d.type),t&&f.active++===0&&f.event.trigger("ajaxStart");if(!d.hasContent){d.data&&(d.url+=(bO.test(d.url)?"&":"?")+d.data,delete d.data),k=d.url;if(d.cache===!1){var x=f.now(),y=d.url.replace(bS,"$1_="+x);d.url=y+(y===d.url?(bO.test(d.url)?"&":"?")+"_="+x:"")}}(d.data&&d.hasContent&&d.contentType!==!1||c.contentType)&&v.setRequestHeader("Content-Type",d.contentType),d.ifModified&&(k=k||d.url,f.lastModified[k]&&v.setRequestHeader("If-Modified-Since",f.lastModified[k]),f.etag[k]&&v.setRequestHeader("If-None-Match",f.etag[k])),v.setRequestHeader("Accept",d.dataTypes[0]&&d.accepts[d.dataTypes[0]]?d.accepts[d.dataTypes[0]]+(d.dataTypes[0]!=="*"?", "+bZ+"; q=0.01":""):d.accepts["*"]);for(u in d.headers)v.setRequestHeader(u,d.headers[u]);if(d.beforeSend&&(d.beforeSend.call(e,v,d)===!1||s===2)){v.abort();return!1}for(u in{success:1,error:1,complete:1})v[u](d[u]);p=ca(bW,d,c,v);if(!p)w(-1,"No Transport");else{v.readyState=1,t&&g.trigger("ajaxSend",[v,d]),d.async&&d.timeout>0&&(q=setTimeout(function(){v.abort("timeout")},d.timeout));try{s=1,p.send(l,w)}catch(z){s<2?w(-1,z):f.error(z)}}return v},param:function(a,c){var d=[],e=function(a,b){b=f.isFunction(b)?b():b,d[d.length]=encodeURIComponent(a)+"="+encodeURIComponent(b)};c===b&&(c=f.ajaxSettings.traditional);if(f.isArray(a)||a.jquery&&!f.isPlainObject(a))f.each(a,function(){e(this.name,this.value)});else for(var g in a)cc(g,a[g],c,e);return d.join("&").replace(bF,"+")}}),f.extend({active:0,lastModified:{},etag:{}});var cf=f.now(),cg=/(\=)\?(&|$)|\?\?/i;f.ajaxSetup({jsonp:"callback",jsonpCallback:function(){return f.expando+"_"+cf++}}),f.ajaxPrefilter("json jsonp",function(b,c,d){var e=b.contentType==="application/x-www-form-urlencoded"&&typeof b.data=="string";if(b.dataTypes[0]==="jsonp"||b.jsonp!==!1&&(cg.test(b.url)||e&&cg.test(b.data))){var g,h=b.jsonpCallback=f.isFunction(b.jsonpCallback)?b.jsonpCallback():b.jsonpCallback,i=a[h],j=b.url,k=b.data,l="$1"+h+"$2";b.jsonp!==!1&&(j=j.replace(cg,l),b.url===j&&(e&&(k=k.replace(cg,l)),b.data===k&&(j+=(/\?/.test(j)?"&":"?")+b.jsonp+"="+h))),b.url=j,b.data=k,a[h]=function(a){g=[a]},d.always(function(){a[h]=i,g&&f.isFunction(i)&&a[h](g[0])}),b.converters["script json"]=function(){g||f.error(h+" was not called");return g[0]},b.dataTypes[0]="json";return"script"}}),f.ajaxSetup({accepts:{script:"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"},contents:{script:/javascript|ecmascript/},converters:{"text script":function(a){f.globalEval(a);return a}}}),f.ajaxPrefilter("script",function(a){a.cache===b&&(a.cache=!1),a.crossDomain&&(a.type="GET",a.global=!1)}),f.ajaxTransport("script",function(a){if(a.crossDomain){var d,e=c.head||c.getElementsByTagName("head")[0]||c.documentElement;return{send:function(f,g){d=c.createElement("script"),d.async="async",a.scriptCharset&&(d.charset=a.scriptCharset),d.src=a.url,d.onload=d.onreadystatechange=function(a,c){if(c||!d.readyState||/loaded|complete/.test(d.readyState))d.onload=d.onreadystatechange=null,e&&d.parentNode&&e.removeChild(d),d=b,c||g(200,"success")},e.insertBefore(d,e.firstChild)},abort:function(){d&&d.onload(0,1)}}}});var ch=a.ActiveXObject?function(){for(var a in cj)cj[a](0,1)}:!1,ci=0,cj;f.ajaxSettings.xhr=a.ActiveXObject?function(){return!this.isLocal&&ck()||cl()}:ck,function(a){f.extend(f.support,{ajax:!!a,cors:!!a&&"withCredentials"in a})}(f.ajaxSettings.xhr()),f.support.ajax&&f.ajaxTransport(function(c){if(!c.crossDomain||f.support.cors){var d;return{send:function(e,g){var h=c.xhr(),i,j;c.username?h.open(c.type,c.url,c.async,c.username,c.password):h.open(c.type,c.url,c.async);if(c.xhrFields)for(j in c.xhrFields)h[j]=c.xhrFields[j];c.mimeType&&h.overrideMimeType&&h.overrideMimeType(c.mimeType),!c.crossDomain&&!e["X-Requested-With"]&&(e["X-Requested-With"]="XMLHttpRequest");try{for(j in e)h.setRequestHeader(j,e[j])}catch(k){}h.send(c.hasContent&&c.data||null),d=function(a,e){var j,k,l,m,n;try{if(d&&(e||h.readyState===4)){d=b,i&&(h.onreadystatechange=f.noop,ch&&delete cj[i]);if(e)h.readyState!==4&&h.abort();else{j=h.status,l=h.getAllResponseHeaders(),m={},n=h.responseXML,n&&n.documentElement&&(m.xml=n),m.text=h.responseText;try{k=h.statusText}catch(o){k=""}!j&&c.isLocal&&!c.crossDomain?j=m.text?200:404:j===1223&&(j=204)}}}catch(p){e||g(-1,p)}m&&g(j,k,m,l)},!c.async||h.readyState===4?d():(i=++ci,ch&&(cj||(cj={},f(a).unload(ch)),cj[i]=d),h.onreadystatechange=d)},abort:function(){d&&d(0,1)}}}});var cm={},cn,co,cp=/^(?:toggle|show|hide)$/,cq=/^([+\-]=)?([\d+.\-]+)([a-z%]*)$/i,cr,cs=[["height","marginTop","marginBottom","paddingTop","paddingBottom"],["width","marginLeft","marginRight","paddingLeft","paddingRight"],["opacity"]],ct;f.fn.extend({show:function(a,b,c){var d,e;if(a||a===0)return this.animate(cw("show",3),a,b,c);for(var g=0,h=this.length;g=i.duration+this.startTime){this.now=this.end,this.pos=this.state=1,this.update(),i.animatedProperties[this.prop]=!0;for(b in i.animatedProperties)i.animatedProperties[b]!==!0&&(g=!1);if(g){i.overflow!=null&&!f.support.shrinkWrapBlocks&&f.each(["","X","Y"],function(a,b){h.style["overflow"+b]=i.overflow[a]}),i.hide&&f(h).hide();if(i.hide||i.show)for(b in i.animatedProperties)f.style(h,b,i.orig[b]),f.removeData(h,"fxshow"+b,!0),f.removeData(h,"toggle"+b,!0);d=i.complete,d&&(i.complete=!1,d.call(h))}return!1}i.duration==Infinity?this.now=e:(c=e-this.startTime,this.state=c/i.duration,this.pos=f.easing[i.animatedProperties[this.prop]](this.state,c,0,1,i.duration),this.now=this.start+(this.end-this.start)*this.pos),this.update();return!0}},f.extend(f.fx,{tick:function(){var a,b=f.timers,c=0;for(;c-1,k={},l={},m,n;j?(l=e.position(),m=l.top,n=l.left):(m=parseFloat(h)||0,n=parseFloat(i)||0),f.isFunction(b)&&(b=b.call(a,c,g)),b.top!=null&&(k.top=b.top-g.top+m),b.left!=null&&(k.left=b.left-g.left+n),"using"in b?b.using.call(a,k):e.css(k)}},f.fn.extend({position:function(){if(!this[0])return null;var a=this[0],b=this.offsetParent(),c=this.offset(),d=cz.test(b[0].nodeName)?{top:0,left:0}:b.offset();c.top-=parseFloat(f.css(a,"marginTop"))||0,c.left-=parseFloat(f.css(a,"marginLeft"))||0,d.top+=parseFloat(f.css(b[0],"borderTopWidth"))||0,d.left+=parseFloat(f.css(b[0],"borderLeftWidth"))||0;return{top:c.top-d.top,left:c.left-d.left}},offsetParent:function(){return this.map(function(){var a=this.offsetParent||c.body;while(a&&!cz.test(a.nodeName)&&f.css(a,"position")==="static")a=a.offsetParent;return a})}}),f.each(["Left","Top"],function(a,c){var d="scroll"+c;f.fn[d]=function(c){var e,g;if(c===b){e=this[0];if(!e)return null;g=cA(e);return g?"pageXOffset"in g?g[a?"pageYOffset":"pageXOffset"]:f.support.boxModel&&g.document.documentElement[d]||g.document.body[d]:e[d]}return this.each(function(){g=cA(this),g?g.scrollTo(a?f(g).scrollLeft():c,a?c:f(g).scrollTop()):this[d]=c})}}),f.each(["Height","Width"],function(a,c){var d=c.toLowerCase();f.fn["inner"+c]=function(){var a=this[0];return a?a.style?parseFloat(f.css(a,d,"padding")):this[d]():null},f.fn["outer"+c]=function(a){var b=this[0];return b?b.style?parseFloat(f.css(b,d,a?"margin":"border")):this[d]():null},f.fn[d]=function(a){var e=this[0];if(!e)return a==null?null:this;if(f.isFunction(a))return this.each(function(b){var c=f(this);c[d](a.call(this,b,c[d]()))});if(f.isWindow(e)){var g=e.document.documentElement["client"+c],h=e.document.body;return e.document.compatMode==="CSS1Compat"&&g||h&&h["client"+c]||g}if(e.nodeType===9)return Math.max(e.documentElement["client"+c],e.body["scroll"+c],e.documentElement["scroll"+c],e.body["offset"+c],e.documentElement["offset"+c]);if(a===b){var i=f.css(e,d),j=parseFloat(i);return f.isNumeric(j)?j:i}return this.css(d,typeof a=="string"?a:a+"px")}}),a.jQuery=a.$=f})(window); -------------------------------------------------------------------------------- /examples/BlinkLed/static/lights.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ovidiucp/TinyWebServer/b86ff2b4e210b97beb3462f96a4b8b0c2a665ace/examples/BlinkLed/static/lights.png -------------------------------------------------------------------------------- /examples/BlinkLed/static/main.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 Ovidiu Predescu 3 | * Date: December 2010 4 | */ 5 | String.prototype.trim = function () { 6 | return this.replace(/^\s*/, "").replace(/\s*$/, ""); 7 | }; 8 | 9 | function Button(elem) { 10 | var btn = this; 11 | this.elem = elem; 12 | this.setEnabled(false); 13 | this.elem.click(function(e) { 14 | e.preventDefault(); 15 | btn.clickHandler(btn, e); 16 | }); 17 | }; 18 | 19 | Button.prototype.setEnabled = function(yesno) { 20 | this.enabled = yesno; 21 | this.elem.attr('class', function(idx, val) { 22 | var newClass = yesno ? "on" : "off"; 23 | return newClass; 24 | }); 25 | }; 26 | 27 | Button.prototype.isEnabled = function() { return this.enabled; }; 28 | 29 | Button.prototype.clickHandler = function(btn, e) { 30 | var url = btn.elem.attr('href'); 31 | $.ajax({type: "POST", 32 | data: (!btn.isEnabled() + 0).toString(10), 33 | dataType: "text", 34 | cache: false, 35 | url: url, 36 | success: function(r) { 37 | }, 38 | error: function(s, xhr, status, e) { 39 | console.log("POST failed: " + s.responseText); 40 | } 41 | }); 42 | }; 43 | 44 | function ledStatus(btn, url) { 45 | $.ajax({type: "GET", 46 | cache: false, 47 | url: url, 48 | success: function(status) { 49 | status = parseInt(status.trim()); 50 | btn.setEnabled(status); 51 | window.setTimeout(function() {ledStatus(btn, url);}, 400); 52 | }, 53 | error: function(s, xhr, status, e) { 54 | console.log("Getting status failed: " 55 | + s.responseText); 56 | } 57 | }); 58 | }; 59 | 60 | $(document).ready( 61 | function() { 62 | var lightBulb = new Button($("#lightbulb")); 63 | ledStatus(lightBulb, "/ledstatus"); 64 | }); 65 | -------------------------------------------------------------------------------- /examples/BlinkLed/static/style.css: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2010 Ovidiu Predescu 3 | Date: December 2010 4 | */ 5 | 6 | div.light a { 7 | margin-left: auto; 8 | margin-right: auto; 9 | background-image:url('lights.png'); 10 | display: block; 11 | } 12 | 13 | a.off { background-position: 0 0; width: 258px; height: 299px; } 14 | a.on { background-position: -308px 0; width: 258px; height: 299px; } 15 | -------------------------------------------------------------------------------- /examples/BlinkLed/upload.bat: -------------------------------------------------------------------------------- 1 | set IPADDRESS=http://192.168.0.88 2 | rem Be sure to change IP address (above) to match your board. No other modification to this file should be needed 3 | @echo off 4 | 5 | rem Run this bat file to load web pages to the SD card on your Arduino Ethernet shield. 6 | rem This file is only needed with windows - otherwise use upload.sh 7 | 8 | set SubDir=static/ 9 | for %%f in (%SubDir%*.*) do ( 10 | echo uploading %SubDir%%%f to %IPADDRESS%/upload/ 11 | curl.exe -0 -T %SubDir%%%f %IPADDRESS%/upload/ 12 | 13 | ) 14 | echo Done! 15 | pause 16 | -------------------------------------------------------------------------------- /examples/BlinkLed/upload.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # Copyright 2010 Ovidiu Predescu 4 | # Date: June 2010 5 | 6 | ARDUINO=192.42.172.237 7 | 8 | if [ $# != 0 ]; then 9 | FILES="$*" 10 | else 11 | FILES=static/* 12 | fi 13 | 14 | for f in $FILES; do 15 | if [[ $(echo $f | egrep "~|CVS") ]]; then 16 | echo Skipping $f 17 | else 18 | size=`ls -l $f | awk '{print $5}'` 19 | echo "Uploading $f ($size bytes)" 20 | curl -0 -T $f http://$ARDUINO/upload/ 21 | fi 22 | done 23 | -------------------------------------------------------------------------------- /examples/FileUpload/FileUpload.ino: -------------------------------------------------------------------------------- 1 | // -*- c++ -*- 2 | // 3 | // Copyright 2010 Ovidiu Predescu 4 | // Date: June 2010 5 | // Updated: 08-JAN-2012 for Arduno IDE 1.0 by 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /****************VALUES YOU CHANGE*************/ 16 | // pin 4 is the SPI select pin for the SDcard 17 | const int SD_CS = 4; 18 | 19 | // pin 10 is the SPI select pin for the Ethernet 20 | const int ETHER_CS = 10; 21 | 22 | // Don't forget to modify the IP to an available one on your home network 23 | byte ip[] = { 192, 168, 5, 177 }; 24 | /*********************************************/ 25 | 26 | static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 27 | 28 | boolean file_handler(TinyWebServer& web_server); 29 | boolean index_handler(TinyWebServer& web_server); 30 | 31 | TinyWebServer::PathHandler handlers[] = { 32 | // Work around Arduino's IDE preprocessor bug in handling /* inside 33 | // strings. 34 | // 35 | // `put_handler' is defined in TinyWebServer 36 | {"/", TinyWebServer::GET, &index_handler }, 37 | {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler }, 38 | {"/" "*", TinyWebServer::GET, &file_handler }, 39 | {NULL}, 40 | }; 41 | 42 | const char* headers[] = { 43 | "Content-Length", 44 | NULL 45 | }; 46 | 47 | TinyWebServer web = TinyWebServer(handlers, headers); 48 | 49 | boolean has_filesystem = true; 50 | Sd2Card card; 51 | SdVolume volume; 52 | SdFile root; 53 | SdFile file; 54 | 55 | void send_file_name(TinyWebServer& web_server, const char* filename) { 56 | if (!filename) { 57 | web_server.send_error_code(404); 58 | web_server << F("Could not parse URL"); 59 | } else { 60 | TinyWebServer::MimeType mime_type 61 | = TinyWebServer::get_mime_type_from_filename(filename); 62 | web_server.send_error_code(200); 63 | web_server.send_content_type(mime_type); 64 | web_server.end_headers(); 65 | if (file.open(&root, filename, O_READ)) { 66 | Serial << F("Read file "); Serial.println(filename); 67 | web_server.send_file(file); 68 | file.close(); 69 | } else { 70 | web_server << F("Could not find file: ") << filename << "\n"; 71 | } 72 | } 73 | } 74 | 75 | boolean file_handler(TinyWebServer& web_server) { 76 | char* filename = TinyWebServer::get_file_from_path(web_server.get_path()); 77 | send_file_name(web_server, filename); 78 | free(filename); 79 | return true; 80 | } 81 | 82 | boolean index_handler(TinyWebServer& web_server) { 83 | send_file_name(web_server, "INDEX.HTM"); 84 | return true; 85 | } 86 | 87 | void file_uploader_handler(TinyWebServer& web_server, 88 | TinyWebPutHandler::PutAction action, 89 | char* buffer, int size) { 90 | static uint32_t start_time; 91 | static uint32_t total_size; 92 | 93 | switch (action) { 94 | case TinyWebPutHandler::START: 95 | start_time = millis(); 96 | total_size = 0; 97 | if (!file.isOpen()) { 98 | // File is not opened, create it. First obtain the desired name 99 | // from the request path. 100 | char* fname = web_server.get_file_from_path(web_server.get_path()); 101 | if (fname) { 102 | Serial << F("Creating ") << fname << "\n"; 103 | file.open(&root, fname, O_CREAT | O_WRITE | O_TRUNC); 104 | free(fname); 105 | } 106 | } 107 | break; 108 | 109 | case TinyWebPutHandler::WRITE: 110 | if (file.isOpen()) { 111 | file.write(buffer, size); 112 | total_size += size; 113 | } 114 | break; 115 | 116 | case TinyWebPutHandler::END: 117 | file.sync(); 118 | Serial << F("Wrote ") << file.fileSize() << F(" bytes in ") 119 | << millis() - start_time << F(" millis (received ") 120 | << total_size << F(" bytes)\n"); 121 | file.close(); 122 | } 123 | } 124 | 125 | void setup() { 126 | Serial.begin(115200); 127 | Serial << F("Free RAM: ") << FreeRam() << "\n"; 128 | 129 | pinMode(SS_PIN, OUTPUT); // set the SS pin as an output 130 | // (necessary to keep the board as 131 | // master and not SPI slave) 132 | digitalWrite(SS_PIN, HIGH); // and ensure SS is high 133 | 134 | // Ensure we are in a consistent state after power-up or a reset 135 | // button These pins are standard for the Arduino w5100 Rev 3 136 | // ethernet board They may need to be re-jigged for different boards 137 | pinMode(ETHER_CS, OUTPUT); // Set the CS pin as an output 138 | digitalWrite(ETHER_CS, HIGH); // Turn off the W5100 chip! (wait for 139 | // configuration) 140 | pinMode(SD_CS, OUTPUT); // Set the SDcard CS pin as an output 141 | digitalWrite(SD_CS, HIGH); // Turn off the SD card! (wait for 142 | // configuration) 143 | 144 | // initialize the SD card. 145 | Serial << F("Setting up SD card...\n"); 146 | // pass over the speed and Chip select for the SD card 147 | if (!card.init(SPI_FULL_SPEED, SD_CS)) { 148 | Serial << F("card failed\n"); 149 | has_filesystem = false; 150 | } 151 | // initialize a FAT volume. 152 | if (!volume.init(&card)) { 153 | Serial << F("vol.init failed!\n"); 154 | has_filesystem = false; 155 | } 156 | if (!root.openRoot(&volume)) { 157 | Serial << F("openRoot failed"); 158 | has_filesystem = false; 159 | } 160 | 161 | if (has_filesystem) { 162 | // Assign our function to `upload_handler_fn'. 163 | TinyWebPutHandler::put_handler_fn = file_uploader_handler; 164 | } 165 | 166 | // Initialize the Ethernet. 167 | Serial << F("Setting up the Ethernet card...\n"); 168 | Ethernet.begin(mac, ip); 169 | 170 | // Start the web server. 171 | Serial << F("Web server starting...\n"); 172 | web.begin(); 173 | 174 | Serial << F("Ready to accept HTTP requests.\n"); 175 | } 176 | 177 | void loop() { 178 | if (has_filesystem) { 179 | web.process(); 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /examples/FileUpload/README.txt: -------------------------------------------------------------------------------- 1 | To get this example running, make sure you modify the IP address 2 | inside the FileUpload.ino and upload.sh files to match an unused IP 3 | address in your network. 4 | 5 | Then run the upload.sh script to upload the files in the static 6 | directory to your Arduino web server. Point a browser to the IP 7 | address you specified above. 8 | 9 | If you're on Windows, make sure you install curl first on your 10 | machine. Then open a terminal and run curl like below for each of the 11 | files in the static/ directory. Replace $ARDUINO with the IP address 12 | of your Arduino and $file with the name of the file you want to 13 | upload. 14 | 15 | curl -0 -T $file http://$ARDUINO/upload/ 16 | 17 | *Note that since the handler in the 'FileUpload.ino' looks like this: 18 | 19 | {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler } 20 | 21 | you must ensure that the path '/upload/' is in your submitted URL 22 | 23 | 24 | If you're a Windows developer, I'd appreciate if you could write a 25 | small batch file that does the equivalent of update.sh. 26 | -------------------------------------------------------------------------------- /examples/FileUpload/html/index.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | Test 4 | 5 | 6 |

It works!

7 | 8 |

A simple HTML file to show how file upload on the Arduino web 9 | server works.

10 | 11 | Lava spewing in the ocean, Big Island, Hawaii 2008 12 | 13 | 14 | -------------------------------------------------------------------------------- /examples/FileUpload/html/lava.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ovidiucp/TinyWebServer/b86ff2b4e210b97beb3462f96a4b8b0c2a665ace/examples/FileUpload/html/lava.jpg -------------------------------------------------------------------------------- /examples/FileUpload/upload.bat: -------------------------------------------------------------------------------- 1 | set IPADDRESS=http://192.168.0.88 2 | rem Be sure to change IP address (above) to match your board. No other modification to this file should be needed 3 | @echo off 4 | 5 | rem Run this bat file to load web pages to the SD card on your Arduino Ethernet shield. 6 | rem This file is only needed with windows - otherwise use upload.sh 7 | 8 | set SubDir=html/ 9 | for %%f in (%SubDir%*.*) do ( 10 | echo uploading %SubDir%%%f to %IPADDRESS%/upload/ 11 | curl.exe -0 -T %SubDir%%%f %IPADDRESS%/upload/ 12 | 13 | ) 14 | echo Done! 15 | pause -------------------------------------------------------------------------------- /examples/FileUpload/upload.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # Copyright 2010 Ovidiu Predescu 4 | # Date: June 2010 5 | 6 | ARDUINO=192.42.172.237 7 | 8 | if [ $# != 0 ]; then 9 | FILES="$*" 10 | else 11 | FILES=html/* 12 | fi 13 | 14 | for f in $FILES; do 15 | if [[ $(echo $f | egrep "~|CVS") ]]; then 16 | echo Skipping $f 17 | else 18 | size=`ls -l $f | awk '{print $5}'` 19 | echo "Uploading $f ($size bytes)" 20 | curl -0 -T $f http://$ARDUINO/upload/ 21 | fi 22 | done 23 | -------------------------------------------------------------------------------- /examples/SimpleWebServer/SimpleWebServer.ino: -------------------------------------------------------------------------------- 1 | // -*- c++ -*- 2 | // 3 | // Copyright 2010 Ovidiu Predescu 4 | // Date: June 2010 5 | // Updated: 08-JAN-2012 for Arduno IDE 1.0 by 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /****************VALUES YOU CHANGE*************/ 16 | // pin 4 is the SPI select pin for the SDcard 17 | const int SD_CS = 4; 18 | 19 | // pin 10 is the SPI select pin for the Ethernet 20 | const int ETHER_CS = 10; 21 | 22 | // Don't forget to modify the IP to an available one on your home network 23 | byte ip[] = { 192, 168, 5, 177 }; 24 | /*********************************************/ 25 | 26 | static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 27 | 28 | boolean index_handler(TinyWebServer& web_server); 29 | 30 | TinyWebServer::PathHandler handlers[] = { 31 | {"/", TinyWebServer::GET, &index_handler }, 32 | {NULL}, 33 | }; 34 | 35 | boolean index_handler(TinyWebServer& web_server) { 36 | web_server.send_error_code(200); 37 | web_server.end_headers(); 38 | web_server << F("

Hello World!

\n"); 39 | return true; 40 | } 41 | 42 | boolean has_ip_address = false; 43 | TinyWebServer web = TinyWebServer(handlers, NULL); 44 | 45 | const char* ip_to_str(const uint8_t* ipAddr) 46 | { 47 | static char buf[16]; 48 | sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); 49 | return buf; 50 | } 51 | 52 | void setup() { 53 | Serial.begin(115200); 54 | Serial << F("Free RAM: ") << FreeRam() << "\n"; 55 | 56 | pinMode(SS_PIN, OUTPUT); // set the SS pin as an output 57 | // (necessary to keep the board as 58 | // master and not SPI slave) 59 | digitalWrite(SS_PIN, HIGH); // and ensure SS is high 60 | 61 | // Ensure we are in a consistent state after power-up or a reset 62 | // button These pins are standard for the Arduino w5100 Rev 3 63 | // ethernet board They may need to be re-jigged for different boards 64 | pinMode(ETHER_CS, OUTPUT); // Set the CS pin as an output 65 | digitalWrite(ETHER_CS, HIGH); // Turn off the W5100 chip! (wait for 66 | // configuration) 67 | pinMode(SD_CS, OUTPUT); // Set the SDcard CS pin as an output 68 | digitalWrite(SD_CS, HIGH); // Turn off the SD card! (wait for 69 | // configuration) 70 | 71 | // Initialize the Ethernet. 72 | Serial << F("Setting up the Ethernet card...\n"); 73 | Ethernet.begin(mac, ip); 74 | 75 | // Start the web server. 76 | Serial << F("Web server starting...\n"); 77 | web.begin(); 78 | 79 | Serial << F("Ready to accept HTTP requests.\n\n"); 80 | } 81 | 82 | void loop() { 83 | web.process(); 84 | } 85 | -------------------------------------------------------------------------------- /examples/WebServerSD/WebServerSD.ino: -------------------------------------------------------------------------------- 1 | // -*- c++ -*- 2 | // 3 | // Copyright 2010 Ovidiu Predescu 4 | // Date: June 2010 5 | // Updated: 08-JAN-2012 for Arduno IDE 1.0 by 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | boolean file_handler(TinyWebServer& web_server); 15 | boolean index_handler(TinyWebServer& web_server); 16 | 17 | boolean has_filesystem = true; 18 | Sd2Card card; 19 | SdVolume volume; 20 | SdFile root; 21 | SdFile file; 22 | 23 | static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 24 | 25 | // Don't forget to modify the IP to an available one on your home network 26 | byte ip[] = { 192, 168, 178, 177 }; 27 | 28 | const int sdChipSelect = 4; // SD card chipSelect 29 | 30 | TinyWebServer::PathHandler handlers[] = { 31 | {"/", TinyWebServer::GET, &index_handler }, 32 | {"/" "*", TinyWebServer::GET, &file_handler }, 33 | {NULL}, 34 | }; 35 | 36 | boolean file_handler(TinyWebServer& web_server) { 37 | 38 | if(!has_filesystem) { 39 | web_server.send_error_code(500); 40 | web_server << F("Internal Server Error"); 41 | return true; 42 | } 43 | 44 | char* filename = TinyWebServer::get_file_from_path(web_server.get_path()); 45 | 46 | if(!filename) { 47 | web_server.send_error_code(400); 48 | web_server << F("Bad Request"); 49 | return true; 50 | } 51 | 52 | send_file_name(web_server, filename); 53 | free(filename); 54 | return true; 55 | } 56 | 57 | void send_file_name(TinyWebServer& web_server, const char* filename) { 58 | 59 | TinyWebServer::MimeType mime_type 60 | = TinyWebServer::get_mime_type_from_filename(filename); 61 | if (file.open(&root, filename, O_READ)) { 62 | web_server.send_error_code(200); 63 | web_server.send_content_type(mime_type); 64 | web_server.end_headers(); 65 | 66 | Serial << F("Read file "); Serial.println(filename); 67 | web_server.send_file(file); 68 | file.close(); 69 | } else { 70 | web_server.send_error_code(404); 71 | web_server.send_content_type("text/plain"); 72 | web_server.end_headers(); 73 | 74 | Serial << F("Could not find file: "); Serial.println(filename); 75 | web_server << F("404 - File not found") << filename << "\n"; 76 | } 77 | } 78 | 79 | boolean index_handler(TinyWebServer& web_server) { 80 | web_server.send_error_code(200); 81 | web_server.end_headers(); 82 | web_server << F("

Hello World!

\n"); 83 | return true; 84 | } 85 | 86 | boolean has_ip_address = false; 87 | TinyWebServer web = TinyWebServer(handlers, NULL); 88 | 89 | const char* ip_to_str(const uint8_t* ipAddr) 90 | { 91 | static char buf[16]; 92 | sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); 93 | return buf; 94 | } 95 | 96 | void setup() { 97 | Serial.begin(9600); 98 | 99 | Serial << F("Free RAM: ") << FreeRam() << "\n"; 100 | 101 | pinMode(10, OUTPUT); // set the SS pin as an output (necessary!) 102 | digitalWrite(10, HIGH); // but turn off the W5100 chip! 103 | // initialize the SD card 104 | 105 | Serial << F("Setting up SD card...\n"); 106 | 107 | if (!card.init(SPI_FULL_SPEED, 4)) { 108 | Serial << F("card failed\n"); 109 | has_filesystem = false; 110 | } 111 | // initialize a FAT volume 112 | if (!volume.init(&card)) { 113 | Serial << F("vol.init failed!\n"); 114 | has_filesystem = false; 115 | } 116 | if (!root.openRoot(&volume)) { 117 | Serial << F("openRoot failed"); 118 | has_filesystem = false; 119 | } 120 | 121 | Serial << F("Setting up the Ethernet card...\n"); 122 | Ethernet.begin(mac, ip); 123 | 124 | // Start the web server. 125 | Serial << F("Web server starting...\n"); 126 | web.begin(); 127 | 128 | Serial << F("Free RAM: ") << FreeRam() << "\n"; 129 | 130 | Serial << F("Ready to accept HTTP requests.\n\n"); 131 | 132 | } 133 | 134 | void loop() { 135 | web.process(); 136 | } 137 | --------------------------------------------------------------------------------