mirror of
https://github.com/sheumann/DiskBrowser.git
synced 2025-02-16 10:31:20 +00:00
Percent-encode query string. Also, display basic error messages.
This commit is contained in:
parent
827dc6fefe
commit
5d59a9d032
@ -49,6 +49,8 @@ char finderRequestName[] = "\pApple~Finder~";
|
|||||||
#define nextPageButton 1012
|
#define nextPageButton 1012
|
||||||
#define mountDiskButton 1013
|
#define mountDiskButton 1013
|
||||||
|
|
||||||
|
#define searchErrorAlert 3000
|
||||||
|
|
||||||
Word resourceFileID;
|
Word resourceFileID;
|
||||||
|
|
||||||
/* ID of our menu item in the Finder (or 0 if we're not active) */
|
/* ID of our menu item in the Finder (or 0 if we're not active) */
|
||||||
@ -64,7 +66,6 @@ Boolean resourceFileOpened, windowOpened;
|
|||||||
boolean gsDisksOnly = true;
|
boolean gsDisksOnly = true;
|
||||||
|
|
||||||
char queryBuf[257];
|
char queryBuf[257];
|
||||||
#define queryString (queryBuf + 1)
|
|
||||||
|
|
||||||
int pageNum = 0;
|
int pageNum = 0;
|
||||||
|
|
||||||
@ -206,16 +207,61 @@ boolean processDoc(json_value *docObj) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *EncodeQueryString(char *queryString) {
|
||||||
|
long sizeNeeded;
|
||||||
|
|
||||||
|
char *encodedStr = NULL;
|
||||||
|
sizeNeeded = strlen(queryString);
|
||||||
|
if (sizeNeeded > 10000)
|
||||||
|
return NULL;
|
||||||
|
encodedStr = malloc(sizeNeeded*3 + 1);
|
||||||
|
if (encodedStr == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
char *s = encodedStr;
|
||||||
|
char c;
|
||||||
|
while ((c = *queryString++) != '\0') {
|
||||||
|
if (c > 0x7F) {
|
||||||
|
//TODO character set translation
|
||||||
|
c = '*';
|
||||||
|
}
|
||||||
|
if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9')) {
|
||||||
|
*s++ = c;
|
||||||
|
} else {
|
||||||
|
snprintf(s, 4, "%%%02X", (unsigned char)c);
|
||||||
|
s+= 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*s = '\0';
|
||||||
|
return encodedStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShowErrorAlert(enum NetDiskError error) {
|
||||||
|
char numStr[6] = "";
|
||||||
|
char *subs[1] = {numStr};
|
||||||
|
|
||||||
|
snprintf(numStr, sizeof(numStr), "%u", error);
|
||||||
|
|
||||||
|
AlertWindow(awResource+awCString+awButtonLayout,
|
||||||
|
(Pointer)subs, searchErrorAlert);
|
||||||
|
}
|
||||||
|
|
||||||
/* Do a search */
|
/* Do a search */
|
||||||
void DoSearch(void) {
|
void DoSearch(void) {
|
||||||
char *searchURL = NULL;
|
char *searchURL = NULL;
|
||||||
int urlLength = 0;
|
int urlLength = 0;
|
||||||
enum NetDiskError result;
|
enum NetDiskError result = 0;
|
||||||
|
|
||||||
WaitCursor();
|
WaitCursor();
|
||||||
|
|
||||||
GetLETextByID(window, searchLine, (StringPtr)&queryBuf);
|
GetLETextByID(window, searchLine, (StringPtr)&queryBuf);
|
||||||
|
|
||||||
|
char *queryString = EncodeQueryString(queryBuf+1);
|
||||||
|
if (queryString == NULL) {
|
||||||
|
result = OUT_OF_MEMORY;
|
||||||
|
goto errorReturn;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
urlLength = snprintf(searchURL, urlLength,
|
urlLength = snprintf(searchURL, urlLength,
|
||||||
"http://archive.org/advancedsearch.php?"
|
"http://archive.org/advancedsearch.php?"
|
||||||
@ -223,23 +269,28 @@ void DoSearch(void) {
|
|||||||
"&fl%%5B%%5D=identifier&fl%%5B%%5D=title"
|
"&fl%%5B%%5D=identifier&fl%%5B%%5D=title"
|
||||||
"&fl%%5B%%5D=emulator_ext"
|
"&fl%%5B%%5D=emulator_ext"
|
||||||
"&rows=%i&page=%i&output=json",
|
"&rows=%i&page=%i&output=json",
|
||||||
gsDisksOnly ? "apple2gs" : "apple2*",
|
gsDisksOnly ? "apple2gs" : "apple2%2A",
|
||||||
queryString,
|
queryString,
|
||||||
DISK_LIST_LENGTH,
|
DISK_LIST_LENGTH,
|
||||||
pageNum);
|
pageNum);
|
||||||
if (urlLength <= 0)
|
if (urlLength <= 0) {
|
||||||
|
result = URL_TOO_LONG;
|
||||||
goto errorReturn;
|
goto errorReturn;
|
||||||
|
}
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
searchURL = malloc(urlLength);
|
searchURL = malloc(urlLength);
|
||||||
if (searchURL == NULL)
|
if (searchURL == NULL) {
|
||||||
|
result = OUT_OF_MEMORY;
|
||||||
goto errorReturn;
|
goto errorReturn;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(queryString);
|
||||||
|
queryString = NULL;
|
||||||
|
|
||||||
result = SetURL(&sess, searchURL, FALSE, FALSE);
|
result = SetURL(&sess, searchURL, FALSE, FALSE);
|
||||||
//TODO enable this once we have real code to build the URL
|
if (result != OPERATION_SUCCESSFUL)
|
||||||
//if (result != OPERATION_SUCCESSFUL)
|
goto errorReturn;
|
||||||
// goto errorReturn;
|
|
||||||
|
|
||||||
result = DoHTTPRequest(&sess);
|
result = DoHTTPRequest(&sess);
|
||||||
if (result != OPERATION_SUCCESSFUL)
|
if (result != OPERATION_SUCCESSFUL)
|
||||||
@ -251,27 +302,37 @@ void DoSearch(void) {
|
|||||||
|
|
||||||
free(netBuf);
|
free(netBuf);
|
||||||
netBuf = malloc(sess.contentLength + 1);
|
netBuf = malloc(sess.contentLength + 1);
|
||||||
if (netBuf == NULL)
|
if (netBuf == NULL) {
|
||||||
|
result = OUT_OF_MEMORY;
|
||||||
goto errorReturn;
|
goto errorReturn;
|
||||||
|
}
|
||||||
|
|
||||||
InitReadTCP(&sess, sess.contentLength, netBuf);
|
InitReadTCP(&sess, sess.contentLength, netBuf);
|
||||||
while (TryReadTCP(&sess) == rsWaiting)
|
while (TryReadTCP(&sess) == rsWaiting)
|
||||||
/* keep reading */ ;
|
/* keep reading */ ;
|
||||||
sess.contentLength -= sess.readCount;
|
sess.contentLength -= sess.readCount;
|
||||||
*(netBuf + (sess.contentLength)) = 0;
|
*(netBuf + sess.contentLength) = 0;
|
||||||
|
|
||||||
if (json)
|
if (json)
|
||||||
json_value_free(json);
|
json_value_free(json);
|
||||||
json = json_parse(netBuf, sess.contentLength);
|
json = json_parse(netBuf, sess.contentLength);
|
||||||
if (json == NULL)
|
if (json == NULL) {
|
||||||
|
result = JSON_PARSING_ERROR;
|
||||||
goto errorReturn;
|
goto errorReturn;
|
||||||
|
}
|
||||||
|
|
||||||
json_value *response = findEntryInObject(json, "response", json_object);
|
json_value *response = findEntryInObject(json, "response", json_object);
|
||||||
if (response == NULL)
|
if (response == NULL) {
|
||||||
|
result = NOT_EXPECTED_CONTENTS;
|
||||||
goto errorReturn;
|
goto errorReturn;
|
||||||
|
}
|
||||||
|
|
||||||
diskListPos = 0;
|
diskListPos = 0;
|
||||||
json_value *docs = findEntryInObject(response, "docs", json_array);
|
json_value *docs = findEntryInObject(response, "docs", json_array);
|
||||||
|
if (docs == NULL) {
|
||||||
|
result = NOT_EXPECTED_CONTENTS;
|
||||||
|
goto errorReturn;
|
||||||
|
}
|
||||||
processArray(docs, json_object, processDoc);
|
processArray(docs, json_object, processDoc);
|
||||||
|
|
||||||
for (int i = 0; i < DISK_LIST_LENGTH; i++) {
|
for (int i = 0; i < DISK_LIST_LENGTH; i++) {
|
||||||
@ -287,7 +348,7 @@ void DoSearch(void) {
|
|||||||
SetCtlMoreFlags(
|
SetCtlMoreFlags(
|
||||||
GetCtlMoreFlags(disksListHandle) | fCtlCanBeTarget | fCtlWantEvents,
|
GetCtlMoreFlags(disksListHandle) | fCtlCanBeTarget | fCtlWantEvents,
|
||||||
disksListHandle);
|
disksListHandle);
|
||||||
HiliteControl(noHilite, GetCtlHandleFromID(window, mountDiskButton));
|
HiliteCtlByID(noHilite, window, mountDiskButton);
|
||||||
|
|
||||||
ShowControl(GetCtlHandleFromID(window, previousPageButton));
|
ShowControl(GetCtlHandleFromID(window, previousPageButton));
|
||||||
ShowControl(GetCtlHandleFromID(window, pageText));
|
ShowControl(GetCtlHandleFromID(window, pageText));
|
||||||
@ -301,11 +362,13 @@ void DoSearch(void) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
errorReturn:
|
errorReturn:
|
||||||
|
NewList2(NULL, 1, (Ref) diskList, refIsPointer,
|
||||||
|
0, (Handle)GetCtlHandleFromID(window, disksList));
|
||||||
|
free(queryString);
|
||||||
free(searchURL);
|
free(searchURL);
|
||||||
EndTCPConnection(&sess);
|
EndTCPConnection(&sess);
|
||||||
InitCursor();
|
InitCursor();
|
||||||
// TODO show error message
|
ShowErrorAlert(result);
|
||||||
SysBeep();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Handle an event after TaskMasterDA processing */
|
/* Handle an event after TaskMasterDA processing */
|
||||||
@ -442,8 +505,8 @@ void ShowBrowserWindow(void) {
|
|||||||
sysWindRecord.memoryID = myUserID;
|
sysWindRecord.memoryID = myUserID;
|
||||||
auxWindInfo->NDASysWindPtr = (Ptr)&sysWindRecord;
|
auxWindInfo->NDASysWindPtr = (Ptr)&sysWindRecord;
|
||||||
|
|
||||||
HiliteControl(inactiveHilite, GetCtlHandleFromID(window, disksList));
|
HiliteCtlByID(inactiveHilite, window, disksList);
|
||||||
HiliteControl(inactiveHilite, GetCtlHandleFromID(window, mountDiskButton));
|
HiliteCtlByID(inactiveHilite, window, mountDiskButton);
|
||||||
|
|
||||||
HideControl(GetCtlHandleFromID(window, previousPageButton));
|
HideControl(GetCtlHandleFromID(window, previousPageButton));
|
||||||
HideControl(GetCtlHandleFromID(window, pageText));
|
HideControl(GetCtlHandleFromID(window, pageText));
|
||||||
|
@ -227,3 +227,16 @@ resource rControlTemplate (mountDiskButton) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
resource rPString(mountDiskButton) { "Mount Disk" };
|
resource rPString(mountDiskButton) { "Mount Disk" };
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Error messages
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define searchErrorAlert 3000
|
||||||
|
|
||||||
|
resource rAlertString (searchErrorAlert) {
|
||||||
|
"52/"
|
||||||
|
"There was an error when searching for disks. (error code *0)"
|
||||||
|
"/^#0\$00"
|
||||||
|
};
|
||||||
|
2
http.c
2
http.c
@ -58,8 +58,8 @@ Boolean BuildHTTPRequest(Session *sess, char *resourceStr) {
|
|||||||
} else {
|
} else {
|
||||||
*s++ = c;
|
*s++ = c;
|
||||||
}
|
}
|
||||||
*s = '\0';
|
|
||||||
}
|
}
|
||||||
|
*s = '\0';
|
||||||
resourceStr = escapedStr;
|
resourceStr = escapedStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,8 +39,11 @@ enum NetDiskError {
|
|||||||
/* File format errors */
|
/* File format errors */
|
||||||
UNSUPPORTED_2IMG_FILE = 600,
|
UNSUPPORTED_2IMG_FILE = 600,
|
||||||
NOT_MULTIPLE_OF_BLOCK_SIZE,
|
NOT_MULTIPLE_OF_BLOCK_SIZE,
|
||||||
NOT_SPECIFIED_IMAGE_TYPE
|
NOT_SPECIFIED_IMAGE_TYPE,
|
||||||
|
|
||||||
|
/* Errors related to proccessing JSON result in the disk browser */
|
||||||
|
JSON_PARSING_ERROR = 900,
|
||||||
|
NOT_EXPECTED_CONTENTS,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user