Skip to content

Commit

Permalink
Merge pull request #14 from ducasp/hub_1.5
Browse files Browse the repository at this point in the history
Hub 1.5
  • Loading branch information
fr3nd authored Sep 19, 2020
2 parents e2524b5 + 766304c commit b1f6609
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 51 deletions.
213 changes: 163 additions & 50 deletions src/hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,25 @@ char tcp_connect(char *conn, char *hostname, unsigned int port) {
*conn = regs.Bytes.B;

n = 0;
sys_timer_hold = *SYSTIMER;
do {
abort_if_esc_is_pressed();
sys_timer_hold = *SYSTIMER;
TCP_WAIT();
abort_if_esc_is_pressed();

while(*SYSTIMER == sys_timer_hold);
n++;
if(n >= TICKS_TO_WAIT) {
return ERR_CUSTOM + ERR_CUSTOM_TIMEOUT;
}
if(*SYSTIMER != sys_timer_hold) {
n++;
if(n >= TICKS_TO_WAIT) {
return ERR_CUSTOM + ERR_CUSTOM_TIMEOUT;
}
sys_timer_hold = *SYSTIMER;
}
regs.Bytes.B = *conn;
regs.Words.HL = 0;
UnapiCall(code_block, TCPIP_TCP_STATE, &regs, REGS_MAIN, REGS_MAIN);
} while((regs.Bytes.A) == 0 && (regs.Bytes.B != 4));
if((regs.Bytes.A) == 0 && (regs.Bytes.B != 4))
TCP_WAIT();
else
break;
} while(1);

if(regs.Bytes.A != 0) {
debug("Error connecting: %i", regs.Bytes.A);
Expand Down Expand Up @@ -279,14 +284,15 @@ char tcp_get(char *conn, data_buffer_t *data_buffer) {
sys_timer_hold = *SYSTIMER;
while(1) {
abort_if_esc_is_pressed();
TCP_WAIT();

if(*SYSTIMER != sys_timer_hold) {
n++;
if(n >= TICKS_TO_WAIT) {
return ERR_CUSTOM + ERR_CUSTOM_TIMEOUT;
}
sys_timer_hold = *SYSTIMER;
}
if(n >= TICKS_TO_WAIT) {
return ERR_CUSTOM + ERR_CUSTOM_TIMEOUT;
}

regs.Bytes.B = *conn;
regs.Words.DE = (int)data_buffer->data;
regs.Words.HL = TCP_BUFFER_SIZE;
Expand All @@ -299,6 +305,8 @@ char tcp_get(char *conn, data_buffer_t *data_buffer) {
data_buffer->size = regs.UWords.BC;
debug("tcp_get: received %i bytes", regs.UWords.BC);
return 0;
} else {
TCP_WAIT();
}
}
}
Expand Down Expand Up @@ -383,47 +391,131 @@ char http_send(char *conn, char *hostname, char *username, char *password, unsig
char buffer[128];
char credentials[128];
char credentialsb64[128];
int l;
int l,retcode;
static unsigned char continue_using_keep_alive = 1;

init_headers_info();
do {
keepingconnectionalive = keepingconnectionalive & continue_using_keep_alive;

init_headers_info();

// Open new connection if:
// * It's not already opened
// * Keepalive is disabled
// * Hostname is different from previous one
if (conn == 0 || keepingconnectionalive == 0 || strcmp(keepalivehostname, hostname) != 0) {
retcode = tcp_connect(conn, hostname, port);
if (retcode != 0) {
die(unapi_strerror(retcode));
}
}

// Open new connection if:
// * It's not already opened
// * Keepalive is disabled
// * Hostname is different from previous one
if (conn == 0 || keepingconnectionalive == 0 || strcmp(keepalivehostname, hostname) != 0) {
run_or_die(tcp_connect(conn, hostname, port));
}
if (trykeepalive) {
strcpy(keepalivehostname, hostname);
}

if (trykeepalive) {
strcpy(keepalivehostname, hostname);
}
sprintf(buffer, "%s %s HTTP/1.1\r\n", method, path);
retcode = tcp_send(conn, buffer);
if (retcode != 0) {
if ( keepingconnectionalive & continue_using_keep_alive ) {
continue_using_keep_alive = 0;
keepingconnectionalive = 0;
continue;
}
else
die(unapi_strerror(retcode));
}

sprintf(buffer, "%s %s HTTP/1.1\r\n", method, path);
run_or_die(tcp_send(conn, buffer));
sprintf(buffer, "Host: %s\r\n", hostname);
retcode = tcp_send(conn, buffer);
if (retcode != 0) {
if ( keepingconnectionalive & continue_using_keep_alive ) {
continue_using_keep_alive = 0;
keepingconnectionalive = 0;
continue;
}
else
die(unapi_strerror(retcode));
}

sprintf(buffer, "Host: %s\r\n", hostname);
run_or_die(tcp_send(conn, buffer));
if (username[0] != '\0' && password[0] != '\0') {
Base64Init(0);
sprintf(credentials, "%s:%s", username, password);
l = Base64EncodeChunk(credentials, credentialsb64, strlen(credentials), 1);
credentialsb64[l] = '\0';
sprintf(buffer, "Authorization: Basic %s\r\n", credentialsb64);
retcode = tcp_send(conn, buffer);
if (retcode != 0) {
if ( keepingconnectionalive & continue_using_keep_alive ) {
continue_using_keep_alive = 0;
keepingconnectionalive = 0;
continue;
}
else
die(unapi_strerror(retcode));
}
}

if (username[0] != '\0' && password[0] != '\0') {
Base64Init(0);
sprintf(credentials, "%s:%s", username, password);
l = Base64EncodeChunk(credentials, credentialsb64, strlen(credentials), 1);
credentialsb64[l] = '\0';
sprintf(buffer, "Authorization: Basic %s\r\n", credentialsb64);
run_or_die(tcp_send(conn, buffer));
}
sprintf(buffer, "User-Agent: MSXHub/%s (MSX-DOS %i; %s)\r\n", MSXHUB_VERSION, msxdosver, unapiver);
retcode = tcp_send(conn, buffer);
if (retcode != 0) {
if ( keepingconnectionalive & continue_using_keep_alive ) {
continue_using_keep_alive = 0;
keepingconnectionalive = 0;
continue;
}
else
die(unapi_strerror(retcode));
}

sprintf(buffer, "User-Agent: MSXHub/%s (MSX-DOS %i; %s)\r\n", MSXHUB_VERSION, msxdosver, unapiver);
run_or_die(tcp_send(conn, buffer));
retcode = tcp_send(conn, "Accept: */*\r\n");
if (retcode != 0) {
if ( keepingconnectionalive & continue_using_keep_alive ) {
continue_using_keep_alive = 0;
keepingconnectionalive = 0;
continue;
}
else
die(unapi_strerror(retcode));
}

if (trykeepalive) {
retcode = tcp_send(conn, "Connection: Keep-Alive\r\n");
if (retcode != 0) {
if ( keepingconnectionalive & continue_using_keep_alive ) {
continue_using_keep_alive = 0;
keepingconnectionalive = 0;
continue;
}
else
die(unapi_strerror(retcode));
}
}
else {
retcode = tcp_send(conn, "Connection: close\r\n");
if (retcode != 0) {
if ( keepingconnectionalive & continue_using_keep_alive ) {
continue_using_keep_alive = 0;
keepingconnectionalive = 0;
continue;
}
else
die(unapi_strerror(retcode));
}
}
retcode = tcp_send(conn, "Accept-Encoding: identity\r\n\r\n");
if (retcode != 0) {
if ( keepingconnectionalive & continue_using_keep_alive ) {
continue_using_keep_alive = 0;
keepingconnectionalive = 0;
continue;
}
else
die(unapi_strerror(retcode));
}
break; //if here, all ran well, no need to retry, just break the do while
} while (1);

run_or_die(tcp_send(conn, "Accept: */*\r\n"));
if (trykeepalive)
run_or_die(tcp_send(conn, "Connection: Keep-Alive\r\n"));
else
run_or_die(tcp_send(conn, "Connection: close\r\n"));
run_or_die(tcp_send(conn, "Accept-Encoding: identity\r\n"));
run_or_die(tcp_send(conn, "\r\n"));
return 0;
}

Expand Down Expand Up @@ -605,7 +697,10 @@ char http_get_content(char *conn, char *hostname, char *username, char *password
die("%s", buffer);
}

if (fp > 4 && fp < 128) { // If it's a regular file: progress bar
if (fp > 4 && fp < 128) {
if (headers_info.content_length>2)
preallocate(fp, headers_info.content_length);
// If it's a regular file: progress bar
printf("\33x5"); // Disable cursor
progress_bar_size = get_screen_size() - 24 - 12;
file_name = (unsigned char*)parse_pathname(0, pathfilename);
Expand All @@ -627,8 +722,7 @@ char http_get_content(char *conn, char *hostname, char *username, char *password
}
sys_timer_hold = *SYSTIMER;
while(bytes_written < headers_info.content_length) {
abort_if_esc_is_pressed();
TCP_WAIT();
abort_if_esc_is_pressed();
if(*SYSTIMER != sys_timer_hold) {
n++;
sys_timer_hold = *SYSTIMER;
Expand All @@ -650,6 +744,8 @@ char http_get_content(char *conn, char *hostname, char *username, char *password
write(data_buffer->data, regs.UWords.BC, fp);
bytes_written += regs.UWords.BC;
progress_bar(regs.UWords.BC, progress_bar_size);
} else {
TCP_WAIT();
}
}
}
Expand Down Expand Up @@ -1256,11 +1352,15 @@ void uninstall(char *package) {
int bytes_read;
char buffer[MAX_PATH_SIZE];
char current_file[MAX_PATH_SIZE];
char undeleted_directories[5][MAX_PATH_SIZE];
char undeleted_directories_count = 0;

if (package[0] == '\0') {
die("Package name not specified.");
}

for (c=0;c<5;++c)
undeleted_directories[c][0]='\0';
// Check if installed

toupper_str(package);
Expand Down Expand Up @@ -1302,7 +1402,11 @@ void uninstall(char *package) {
if (c == 0xD7) { // File does not exist. Continue
printf("WARNING: File %s does not exist...\r\n", current_file);
} else if (c == 0xD0) { // Directory not empty. Continue
printf("WARNING: Directory %s not empty. Not deleting.\r\n", current_file);
printf("WARNING: Directory %s not empty. Not deleting now, will try later...\r\n", current_file);
if (undeleted_directories_count<5) {
strcpy(undeleted_directories[undeleted_directories_count], current_file);
++undeleted_directories_count;
}
} else { // Another error
printf("Error deleting file %s: 0x%X\r\n", current_file, c);
explain(buffer, c);
Expand All @@ -1319,6 +1423,15 @@ void uninstall(char *package) {
}
close(fp);

if (undeleted_directories_count) {
for (n=0;n<undeleted_directories_count;++n) {
c = delete_file(undeleted_directories[n]);
if (c == 0xD0) { // Directory not empty yet
printf("WARNING: Directory %s not empty. Not deleting it...\r\n", undeleted_directories[n]);
}
}
}

// Remove file in idb
strcpy(buffer, configpath);
strcat(buffer, "\\IDB\\");
Expand Down
44 changes: 44 additions & 0 deletions src/include/dos.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,50 @@ int create(char *fn, char mode, char attributes) __naked {
__endasm;
}

void preallocate(int fp, long allocationsize) __naked {
fp;
allocationsize;
__asm
push ix
ld ix,#4
add ix,sp

ld l,2(ix)
ld h,3(ix)
xor a
ld bc,#0x0001
sbc hl,bc
ld e,4(ix)
ld d,5(ix)
jr nc, 00001$
dec de
00001$:
ld b,0(ix)
ld c, SEEK
DOSCALL

ld b,0(ix)
ld de,#0x8000
ld hl,#0x0001
ld c, WRITE
DOSCALL

ld b,0(ix)
xor a
ld de,#0x0000
ld hl,#0x0000
ld c, SEEK
DOSCALL

ld b,0(ix)
ld c, ENSURE
DOSCALL

pop ix
ret
__endasm;
}

int close(int fp) __naked {
fp;
__asm
Expand Down
3 changes: 3 additions & 0 deletions src/include/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#define OPEN #0x43
#define CREATE #0x44
#define CLOSE #0x45
#define ENSURE #0x46
#define READ #0x48
#define WRITE #0x49
#define SEEK #0x4A
#define IOCTL #0x4B
#define DELETE #0x4D
#define GETCD #0x59
Expand All @@ -36,6 +38,7 @@ int putchar(int c);
char get_current_drive(void);
int open(char *fn, char mode);
int create(char *fn, char mode, char attributes);
void preallocate(int fp, long allocationsize);
int close(int fp);
int read(char* buf, unsigned int size, char fp);
unsigned int write(char* buf, unsigned int size, int fp);
Expand Down
2 changes: 1 addition & 1 deletion src/include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "asm.h"

#define MSXHUB_VERSION "1.0.4"
#define MSXHUB_VERSION "1.0.5"

/* DOS errors */
#define NOFIL 0xD7
Expand Down

0 comments on commit b1f6609

Please sign in to comment.