LuaHTTPS.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package org.love2d.luahttps;
  2. import android.text.TextUtils;
  3. import android.util.Log;
  4. import androidx.annotation.Keep;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. @Keep
  17. class LuaHTTPS {
  18. static private String TAG = "LuaHTTPS";
  19. private String urlString;
  20. private byte[] postData;
  21. private byte[] response;
  22. private int responseCode;
  23. private HashMap<String, String> headers;
  24. public LuaHTTPS() {
  25. headers = new HashMap<String, String>();
  26. reset();
  27. }
  28. public void reset() {
  29. urlString = null;
  30. postData = null;
  31. response = null;
  32. responseCode = 0;
  33. headers.clear();
  34. }
  35. @Keep
  36. public void setUrl(String url) {
  37. urlString = url;
  38. }
  39. @Keep
  40. public void setPostData(byte[] postData) {
  41. this.postData = postData;
  42. }
  43. @Keep
  44. public void addHeader(String key, String value) {
  45. headers.put(key, value);
  46. }
  47. @Keep
  48. public String[] getInterleavedHeaders() {
  49. ArrayList<String> resultInterleaved = new ArrayList<String>();
  50. for (Map.Entry<String, String> header: headers.entrySet()) {
  51. String key = header.getKey();
  52. String value = header.getValue();
  53. if (key != null && value != null) {
  54. resultInterleaved.add(key);
  55. resultInterleaved.add(value);
  56. }
  57. }
  58. String[] result = new String[resultInterleaved.size()];
  59. resultInterleaved.toArray(result);
  60. return result;
  61. }
  62. @Keep
  63. public int getResponseCode() {
  64. return responseCode;
  65. }
  66. @Keep
  67. public byte[] getResponse() {
  68. return response;
  69. }
  70. @Keep
  71. public boolean request() {
  72. if (urlString == null) {
  73. return false;
  74. }
  75. URL url;
  76. try {
  77. url = new URL(urlString);
  78. if (!url.getProtocol().equals("http") && !url.getProtocol().equals("https")) {
  79. return false;
  80. }
  81. } catch (MalformedURLException e) {
  82. Log.e(TAG, "Error", e);
  83. return false;
  84. }
  85. HttpURLConnection connection;
  86. try {
  87. connection = (HttpURLConnection) url.openConnection();
  88. } catch (IOException e) {
  89. Log.e(TAG, "Error", e);
  90. return false;
  91. }
  92. // Set header
  93. for (Map.Entry<String, String> headerData: headers.entrySet()) {
  94. connection.setRequestProperty(headerData.getKey(), headerData.getValue());
  95. }
  96. // Set post data
  97. if (postData != null) {
  98. connection.setDoOutput(true);
  99. connection.setChunkedStreamingMode(0);
  100. try {
  101. OutputStream out = connection.getOutputStream();
  102. out.write(postData);
  103. } catch (Exception e) {
  104. Log.e(TAG, "Error", e);
  105. connection.disconnect();
  106. return false;
  107. }
  108. }
  109. // Request
  110. try {
  111. InputStream in;
  112. // Set response code
  113. responseCode = connection.getResponseCode();
  114. if (responseCode >= 400) {
  115. in = connection.getErrorStream();
  116. } else {
  117. in = connection.getInputStream();
  118. }
  119. // Read response
  120. int readed;
  121. byte[] temp = new byte[4096];
  122. ByteArrayOutputStream response = new ByteArrayOutputStream();
  123. while ((readed = in.read(temp)) != -1) {
  124. response.write(temp, 0, readed);
  125. }
  126. this.response = response.toByteArray();
  127. response.close();
  128. // Read headers
  129. headers.clear();
  130. for (Map.Entry<String, List<String>> header: connection.getHeaderFields().entrySet()) {
  131. headers.put(header.getKey(), TextUtils.join(", ", header.getValue()));
  132. }
  133. } catch (Exception e) {
  134. Log.e(TAG, "Error", e);
  135. connection.disconnect();
  136. return false;
  137. }
  138. connection.disconnect();
  139. return true;
  140. }
  141. }