Http.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe;
  23. import haxe.io.Bytes;
  24. typedef Http = HttpFlash;
  25. class HttpFlash extends haxe.http.HttpBase {
  26. var req:flash.net.URLLoader;
  27. /**
  28. Cancels `this` Http request if `request` has been called and a response
  29. has not yet been received.
  30. **/
  31. public function cancel() {
  32. if (req == null)
  33. return;
  34. req.close();
  35. req = null;
  36. }
  37. public override function request(?post:Bool) {
  38. responseAsString = null;
  39. responseBytes = null;
  40. var loader = req = new flash.net.URLLoader();
  41. loader.dataFormat = BINARY;
  42. loader.addEventListener("complete", function(e) {
  43. req = null;
  44. success(Bytes.ofData(loader.data));
  45. });
  46. loader.addEventListener("httpStatus", function(e:flash.events.HTTPStatusEvent) {
  47. // on Firefox 1.5, Flash calls onHTTPStatus with 0 (!??)
  48. if (e.status != 0)
  49. onStatus(e.status);
  50. });
  51. loader.addEventListener("ioError", function(e:flash.events.IOErrorEvent) {
  52. req = null;
  53. responseBytes = Bytes.ofData(loader.data);
  54. onError(e.text);
  55. });
  56. loader.addEventListener("securityError", function(e:flash.events.SecurityErrorEvent) {
  57. req = null;
  58. onError(e.text);
  59. });
  60. // headers
  61. var param = false;
  62. var vars = new flash.net.URLVariables();
  63. for (p in params) {
  64. param = true;
  65. Reflect.setField(vars, p.name, p.value);
  66. }
  67. var small_url = url;
  68. if (param && !post) {
  69. var k = url.split("?");
  70. if (k.length > 1) {
  71. small_url = k.shift();
  72. vars.decode(k.join("?"));
  73. }
  74. }
  75. // Bug in flash player 9 ???
  76. small_url.split("xxx");
  77. var request = new flash.net.URLRequest(small_url);
  78. for (h in headers)
  79. request.requestHeaders.push(new flash.net.URLRequestHeader(h.name, h.value));
  80. if (postData != null) {
  81. request.data = postData;
  82. request.method = "POST";
  83. } else if (postBytes != null) {
  84. request.data = postBytes.getData();
  85. request.method = "POST";
  86. } else {
  87. request.data = vars;
  88. request.method = if (post) "POST" else "GET";
  89. }
  90. try {
  91. loader.load(request);
  92. } catch (e:Dynamic) {
  93. req = null;
  94. onError("Exception: " + Std.string(e));
  95. }
  96. }
  97. }