Skip to content

Commit

Permalink
iPhone compatibility fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyp7 committed Nov 11, 2017
1 parent 7bd80b1 commit 00801bd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 75 deletions.
45 changes: 19 additions & 26 deletions wifi_manager/main/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function startRefreshAPInterval(){
refreshAPInterval = setInterval(refreshAP, 2800);
}

$(function() {
$(document).ready(function(){


$("#wifi-status").on("click", ".ape", function() {
Expand All @@ -66,7 +66,7 @@ $(function() {
$( "#wifi" ).slideDown( "fast", function() {});
});

$(document).on("click", "#join", function() {
$("#join").on("click", function() {
performConnect();
});

Expand Down Expand Up @@ -112,13 +112,13 @@ $(function() {
$( "#connect-details-wrap" ).removeClass('blur');

$.ajax({
url: '/connect',
type: 'DELETE',
success: function(result) {
}
url: '/connect.json',
dataType: 'json',
method: 'DELETE',
cache: false,
data: { 'timestamp': Date.now()}
});

startCheckStatusInterval();

$( "#connect-details" ).slideUp( "fast", function() {});
Expand Down Expand Up @@ -166,23 +166,17 @@ function performConnect(){
$( "#connect-wait" ).slideDown( "fast", function() {});



var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(this.status == 200){
//start refreshing every now and then the IP page
//to see if the connection to the STA is made
}
else{
//alert(this.responseText);
}
};

var pwd = $("#pwd").val();
xhr.open("POST", "/connect", true);
xhr.setRequestHeader('Authorization', "\x02{0}\x03\x02{1}\x03".format(selectedSSID, pwd));
xhr.send();

$.ajax({
url: '/connect.json',
dataType: 'json',
method: 'POST',
cache: false,
headers: { 'X-Custom-ssid': selectedSSID, 'X-Custom-pwd': pwd },
data: { 'timestamp': Date.now()}
});


//now we can re-set the intervals regardless of result
startCheckStatusInterval();
startRefreshAPInterval();
Expand Down Expand Up @@ -225,7 +219,6 @@ function refreshAP(){
function refreshAPHTML(data){
var h = "";
data.forEach(function(e, idx, array) {
//<div class="ape brdb"><div class="w0"><div class="pw">a0308</div></div></div>
h += '<div class="ape{0}"><div class="{1}"><div class="{2}">{3}</div></div></div>'.format(idx === array.length - 1?'':' brdb', rssiToIcon(e.rssi), e.auth==0?'':'pw',e.ssid);
h += "\n";
});
Expand All @@ -237,7 +230,7 @@ function refreshAPHTML(data){


function checkStatus(){
$.getJSON( "/status", function( data ) {
$.getJSON( "/status.json", function( data ) {
if(data.hasOwnProperty('ssid') && data['ssid'] != ""){
if(data["ssid"] === selectedSSID){
//that's a connection attempt
Expand Down
91 changes: 42 additions & 49 deletions wifi_manager/main/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ const static char http_html_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/html\n\
const static char http_css_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/css\nCache-Control: public, max-age=31536000\n\n";
const static char http_js_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/javascript\n\n";
const static char http_jquery_gz_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/javascript\nAccept-Ranges: bytes\nContent-Length: 29995\nContent-Encoding: gzip\n\n";
const static char http_json_hdr[] = "HTTP/1.1 200 OK\nContent-type: application/json\n\n";
const static char http_400_hdr[] = "HTTP/1.1 400 Bad Request\nContent-Length: 0\n\n";
const static char http_404_hdr[] = "HTTP/1.1 404 Not Found\nContent-Length: 0\n\n";
const static char http_503_hdr[] = "HTTP/1.1 503 Service Unavailable\nContent-Length: 0\n\n";
const static char http_ok_json_no_cache_hdr[] = "HTTP/1.1 200 OK\nContent-type: application/json\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\n\n";



Expand Down Expand Up @@ -139,25 +139,28 @@ void http_server(void *pvParameters) {
}


char* http_server_get_authorization_token(char *buffer, int *len) {
char* http_server_get_header(char *request, char *header_name, int *len) {
const char new_line[2] = "\n";
*len = 0;
char* ptr = buffer;
char* ret = NULL;
while (*ptr != '\0' && *ptr != '\n') {
if (*ptr == '\x02') {
ret = ++ptr;
/* find the end of text char. To avoid out of bounds, new lines and 0x00 are considered showstoppers as well. */
while (*ptr != '\x03' && *ptr != '\n' && *ptr != '\0') {
(*len)++;
ptr++;
}
return ret;
char *ret = NULL;
char *ptr = NULL;

ptr = strstr(request, header_name);
if (ptr) {
ret = ptr + strlen(header_name);
ptr = ret;
while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r') {
(*len)++;
ptr++;
}
ptr++;
return ret;
}

return NULL;

}


void http_server_netconn_serve(struct netconn *conn) {

struct netbuf *inbuf;
Expand Down Expand Up @@ -193,7 +196,7 @@ void http_server_netconn_serve(struct netconn *conn) {
else if(strstr(line, "GET /ap.json ")) {
/* if we can get the mutex, write the last version of the AP list */
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
netconn_write(conn, http_json_hdr, sizeof(http_json_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
char *buff = wifi_manager_get_ap_list_json();
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
wifi_manager_unlock_json_buffer();
Expand All @@ -211,11 +214,11 @@ void http_server_netconn_serve(struct netconn *conn) {
netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /status ")){
else if(strstr(line, "GET /status.json ")){
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
char *buff = wifi_manager_get_ip_info_json();
if(buff){
netconn_write(conn, http_json_hdr, sizeof(http_json_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
wifi_manager_unlock_json_buffer();
}
Expand All @@ -230,54 +233,44 @@ void http_server_netconn_serve(struct netconn *conn) {
#endif
}
}
else if(strstr(line, "DELETE /connect ")) {
else if(strstr(line, "DELETE /connect.json ")) {
#if WIFI_MANAGER_DEBUG
printf("http_server_netconn_serve: DELETE /connect\n");
printf("http_server_netconn_serve: DELETE /connect.json\n");
#endif
/* request a disconnection from wifi and forget about it */
wifi_manager_disconnect_async();
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
}
else if(strstr(line, "POST /connect ")) {
else if(strstr(line, "POST /connect.json ")) {
#if WIFI_MANAGER_DEBUG
printf("http_server_netconn_serve: POST /connect\n");
printf("http_server_netconn_serve: POST /connect.json\n");
#endif

bool found = false;
while((line = strtok_r(save_ptr, new_line, &save_ptr))){
if(strstr(line, "Authorization:")){
int lenS = 0, lenP = 0;
char *ssid = http_server_get_authorization_token(line, &lenS);
char *password = NULL;
if(ssid && lenS <= MAX_SSID_SIZE){
password = http_server_get_authorization_token(ssid, &lenP);
if(password && lenP <= MAX_PASSWORD_SIZE){
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
memset(config, 0x00, sizeof(wifi_config_t));
memcpy(config->sta.ssid, ssid, lenS);
memcpy(config->sta.password, password, lenP);
int lenS = 0, lenP = 0;
char *ssid = NULL, *password = NULL;
ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);

if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE){
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
memset(config, 0x00, sizeof(wifi_config_t));
memcpy(config->sta.ssid, ssid, lenS);
memcpy(config->sta.password, password, lenP);

#if WIFI_MANAGER_DEBUG
printf("http_server_netconn_serve: wifi_manager_connect_async() call\n");
printf("http_server_netconn_serve: wifi_manager_connect_async() call\n");
#endif
//initialize connection sequence
wifi_manager_connect_async();
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY); //200ok
found = true;
}
else{
break; /* no point going further, this request is bad and won't be processed */
}
}
else{
break; /* no point going further, this request is bad and won't be processed */
}
break; /* found the Authorization header, no point going further */
}
wifi_manager_connect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
found = true;
}

if(!found){
/* bad request the authentification header is not complete/not the correct format */
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
}

}
else{
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
Expand Down
3 changes: 3 additions & 0 deletions wifi_manager/main/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void http_server(void *pvParameters);
void http_server_netconn_serve(struct netconn *conn);
void http_server_set_event_start();


char* http_server_get_header(char *request, char *header_name, int *len);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 00801bd

Please sign in to comment.