📄 httplib.c
字号:
ret = NET_WRITE_HEADER; } } // Could not connect socket else { ret = NET_SOCKET_CONNECT; } // Close socket if not to keep it open if (mode == CLOSE) { NetLibSocketClose(global -> NetLibRefNum, sr, global -> NetLibTimeout, &err); } return ret;}//---------------------------------------------------------------------//// Parses an URL: find server name and file path////---------------------------------------------------------------------static void HttpParseURL(char *url, UInt16 *port, char **phostname, char **pfilepath){ char *pc, *pp, c; // Default port = 80 *port = 80; // Skip over "http://" url += 7; // Find first char that delimits host part for (pc=url, c=*pc; (c && c!=':' && c!='/'); ) { c = *pc++; } // Separate the parts *(pc-1) = 0; // Get host name *phostname = url; // Get a port number? if (c == ':') { pp = pc; for (c=*pc; (c && c!='/'); ) { c = *pc++; } *(pc-1) = 0; *port = StrAToI(pp); } // Get path *pfilepath = c ? pc : "";}//---------------------------------------------------------------------//// Open the Net Library on the PalmOS and keep its reference////---------------------------------------------------------------------Err HTTPLib_httpopn(UInt16 refnum){ Err err; UInt16 netIFErr; // Get current global pointer HTTPLib_globals * global = HttpGetGlobals(refnum); if (!global) { // Looks like an error return 1; } // Find the net library global -> NetLibRefNum = 0; err = SysLibFind("Net.lib", &(global -> NetLibRefNum)); // Set time-out to 2 secs global -> NetLibTimeout = 10 * SysTicksPerSecond(); // If found open the library if (!err) { err = NetLibOpen(global -> NetLibRefNum, &netIFErr); // Some errors are not useful if (err == netErrAlreadyOpen) { err = 0; } // IF error, terminate if (netIFErr != 0) { err = netErrNoInterfaces; NetLibClose(global -> NetLibRefNum, true); } } return err;}//---------------------------------------------------------------------//// Close the Net Library on the PalmOS////---------------------------------------------------------------------Err HTTPLib_httpcls(UInt16 refnum){ // Get current global pointer HTTPLib_globals * global = HttpGetGlobals(refnum); if (!global) { // Looks like an error return 1; } return NetLibClose(global -> NetLibRefNum, false);}//---------------------------------------------------------------------//// Send a GET or HEAD query and returns results// If pdata is NULL, only header info is returned (lenth and MIME type)////---------------------------------------------------------------------Err HTTPLib_httpget(UInt16 refnum, int *code, char *url, char **pdata, Int32 *plength, char *typebuf){ char * hostname; UInt16 port; char * filepath; char header[MAXBUF]; NetSocketRef sr; Int32 n, length = -1, count; Err err; // Get current global pointer HTTPLib_globals * global = HttpGetGlobals(refnum); if (!global) { // Looks like an error return 1; } // Initializations if (plength) { *plength = 0; } if (typebuf) { *typebuf = 0; } // Parse URL HttpParseURL(url, &port, &hostname, &filepath); // Get full data if requested if (pdata) { *code = HttpQuery(global, hostname, port, "GET", filepath, "", KEEP_OPEN, NULL, 0, &sr); } // Or only headers (faster) else { *code = HttpQuery(global, hostname, port, "HEAD", filepath, "", KEEP_OPEN, NULL, 0, &sr); } // Result is OK if (*code == 200) { // Loop on header while (1) { // Get next header line n = HttpReadLine(global, sr, header, MAXBUF-1); // Net error if (n <= 0) { NetLibSocketClose(global -> NetLibRefNum, sr, global -> NetLibTimeout, &err); *code = NET_READ_HEADER; return 0; } // Empty line marks the end of the headers if (n>0 && (*header)=='\0') { break; } // Parse known headers: length & type if (!StrNCaselessCompare(header, "content-length: ", 16)) { length = StrAToI(header + 16); } // Get type if requested if (typebuf) { if (!StrNCaselessCompare(header, "content-type: ", 14)) { StrCopy(typebuf, header + 14); } } } // Got no length from the header: bad stuff if (length <= 0) { NetLibSocketClose(global -> NetLibRefNum, sr, global -> NetLibTimeout, &err); *code = NET_INVALID_LENGTH; return 0; } // Save length for caller if requested if (plength) { *plength = length; } // Get the data if requested if (pdata && length > 0) { // Allocate some memory for it *pdata = (char *)MemPtrNew(length+1); // Check if successful if (!(*pdata)) { NetLibSocketClose(global -> NetLibRefNum, sr, global -> NetLibTimeout, &err); *code = NET_ALLOC_MEMORY; return 0; } // Read data count = length; HttpReadData(global, sr, *pdata, &count); // Did we read all of it ? if (count != length) { *code = NET_READ_DATA; } (*pdata)[length] = 0; } } // Other HTTP return code: close socket if (*code >= 0) { // Dump pending data if any if (*code >= 0) { HttpDumpData(global, sr); } // Close socket NetLibSocketClose(global -> NetLibRefNum, sr, global -> NetLibTimeout, &err); } return 0;}//---------------------------------------------------------------------//// Send a GET query and returns information// Leaves the connection open for further reads////---------------------------------------------------------------------Err HTTPLib_httpreqopen(UInt16 refnum, int *code, NetSocketRef *psr, char *url, Int32 *plength, char *typebuf){ char * hostname; UInt16 port; char * filepath; char header[MAXBUF]; Int32 n, length = -1; Err err; // Get current global pointer HTTPLib_globals * global = HttpGetGlobals(refnum); if (!global) { // Looks like an error return 1; } // Initializations if (plength) { *plength = 0; } if (typebuf) { *typebuf = 0; } // Parse URL HttpParseURL(url, &port, &hostname, &filepath); // Get full data *code = HttpQuery(global, hostname, port, "GET", filepath, "", KEEP_OPEN, NULL, 0, psr); // Result is OK: get headers if (*code == 200) { // Loop on header while (1) { // Get next header line n = HttpReadLine(global, *psr, header, MAXBUF-1); // Net error if (n <= 0) { NetLibSocketClose(global -> NetLibRefNum, *psr, global -> NetLibTimeout, &err); *code = NET_READ_HEADER; return 0; } // Empty line marks the end of the headers if (n>0 && (*header)=='\0') { break; } // Parse known headers: length & type if (!StrNCaselessCompare(header, "content-length: ", 16)) { length = StrAToI(header + 16); } // Get type if requested if (typebuf) { if (!StrNCaselessCompare(header, "content-type: ", 14)) { StrCopy(typebuf, header + 14); } } } // Save length for caller if requested if (plength) { *plength = length; } } else { // Dump pending data if any if (*code >= 0) { HttpDumpData(global, *psr); } // Close socket NetLibSocketClose(global -> NetLibRefNum, *psr, global -> NetLibTimeout, &err); } return 0;}//---------------------------------------------------------------------//// Read data from a GET request previously submitted (HTTPLib_httpopen)////---------------------------------------------------------------------Err HTTPLib_httpreqread(UInt16 refnum, NetSocketRef *psr, char *pdata, Int32 *plength){ // Get current global pointer HTTPLib_globals * global = HttpGetGlobals(refnum); if (!global) { // Looks like an error return 1; } // Read data return HttpReadData(global, *psr, pdata, plength);}//---------------------------------------------------------------------//// Close an open socket (after reading result from a GET request)////---------------------------------------------------------------------Err HTTPLib_httpreqclose(UInt16 refnum, NetSocketRef *psr){ Err err; // Get current global pointer HTTPLib_globals * global = HttpGetGlobals(refnum); if (!global) { // Looks like an error return 1; } // Close socket NetLibSocketClose(global -> NetLibRefNum, *psr, global -> NetLibTimeout, &err); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -