static void testIsTableEmpty(MprTestGroup *gp) { MprHash *table; table = mprCreateHash(0, 0); assert(table != 0); assert(mprGetHashLength(table) == 0); assert(mprGetFirstKey(table) == 0); assert(mprLookupKey(table, "") == 0); }
static void testInsertAndRemoveHash(MprTestGroup *gp) { MprHash *table; MprKey *sp; cchar *str; int rc; table = mprCreateHash(0, MPR_HASH_STATIC_KEYS | MPR_HASH_STATIC_VALUES); assert(table != 0); /* Single insert */ sp = mprAddKey(table, "Peter", "123 Madison Ave"); assert(sp != 0); sp = mprGetFirstKey(table); assert(sp != 0); assert(sp->key != 0); assert(strcmp(sp->key, "Peter") == 0); assert(sp->data != 0); assert(strcmp(sp->data, "123 Madison Ave") == 0); /* Lookup */ str = mprLookupKey(table, "Peter"); assert(str != 0); assert(strcmp(str, "123 Madison Ave") == 0); rc = mprRemoveKey(table, "Peter"); assert(rc == 0); assert(mprGetHashLength(table) == 0); sp = mprGetFirstKey(table); assert(sp == 0); str = mprLookupKey(table, "Peter"); assert(str == 0); }
static void testIterateHash(MprTestGroup *gp) { MprHash *table; MprKey *sp; char name[80], address[80]; cchar *where; int count, i, check[HASH_COUNT]; table = mprCreateHash(HASH_COUNT, 0); memset(check, 0, sizeof(check)); /* Fill the table */ for (i = 0; i < HASH_COUNT; i++) { mprSprintf(name, sizeof(name), "Bit longer name.%d", i); mprSprintf(address, sizeof(address), "%d Park Ave", i); sp = mprAddKey(table, name, sclone(address)); assert(sp != 0); } assert(mprGetHashLength(table) == HASH_COUNT); /* Check data entered into the table */ sp = mprGetFirstKey(table); count = 0; while (sp) { assert(sp != 0); where = sp->data; assert(isdigit((int) where[0]) != 0); i = atoi(where); check[i] = 1; sp = mprGetNextKey(table, sp); count++; } assert(count == HASH_COUNT); count = 0; for (i = 0; i < HASH_COUNT; i++) { if (check[i]) { count++; } } assert(count == HASH_COUNT); }
/* function get headers(): Object */ static EjsPot *http_headers(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { MprHash *hash; MprKey *kp; EjsPot *results; int i; if (!waitForResponseHeaders(hp)) { return 0; } results = ejsCreateEmptyPot(ejs); hash = httpGetHeaderHash(hp->conn); if (hash == 0) { return results; } for (i = 0, kp = mprGetFirstKey(hash); kp; kp = mprGetNextKey(hash, kp), i++) { ejsSetPropertyByName(ejs, results, EN(kp->key), ejsCreateStringFromAsc(ejs, kp->data)); } return results; }
PUBLIC void httpWriteHeaders(HttpQueue *q, HttpPacket *packet) { Http *http; HttpConn *conn; HttpTx *tx; HttpUri *parsedUri; MprKey *kp; MprBuf *buf; int level; assert(packet->flags == HTTP_PACKET_HEADER); conn = q->conn; http = conn->http; tx = conn->tx; buf = packet->content; if (tx->flags & HTTP_TX_HEADERS_CREATED) { return; } tx->flags |= HTTP_TX_HEADERS_CREATED; tx->responded = 1; if (conn->headersCallback) { /* Must be before headers below */ (conn->headersCallback)(conn->headersCallbackArg); } if (tx->flags & HTTP_TX_USE_OWN_HEADERS && !conn->error) { conn->keepAliveCount = 0; return; } setHeaders(conn, packet); if (conn->endpoint) { mprPutStringToBuf(buf, conn->protocol); mprPutCharToBuf(buf, ' '); mprPutIntToBuf(buf, tx->status); mprPutCharToBuf(buf, ' '); mprPutStringToBuf(buf, httpLookupStatus(http, tx->status)); } else { mprPutStringToBuf(buf, tx->method); mprPutCharToBuf(buf, ' '); parsedUri = tx->parsedUri; if (http->proxyHost && *http->proxyHost) { if (parsedUri->query && *parsedUri->query) { mprPutToBuf(buf, "http://%s:%d%s?%s %s", http->proxyHost, http->proxyPort, parsedUri->path, parsedUri->query, conn->protocol); } else { mprPutToBuf(buf, "http://%s:%d%s %s", http->proxyHost, http->proxyPort, parsedUri->path, conn->protocol); } } else { if (parsedUri->query && *parsedUri->query) { mprPutToBuf(buf, "%s?%s %s", parsedUri->path, parsedUri->query, conn->protocol); } else { mprPutStringToBuf(buf, parsedUri->path); mprPutCharToBuf(buf, ' '); mprPutStringToBuf(buf, conn->protocol); } } } if ((level = httpShouldTrace(conn, HTTP_TRACE_TX, HTTP_TRACE_FIRST, tx->ext)) >= mprGetLogLevel(tx)) { mprAddNullToBuf(buf); mprLog(level, " %s", mprGetBufStart(buf)); } mprPutStringToBuf(buf, "\r\n"); /* Output headers */ kp = mprGetFirstKey(conn->tx->headers); while (kp) { mprPutStringToBuf(packet->content, kp->key); mprPutStringToBuf(packet->content, ": "); if (kp->data) { mprPutStringToBuf(packet->content, kp->data); } mprPutStringToBuf(packet->content, "\r\n"); kp = mprGetNextKey(conn->tx->headers, kp); } /* By omitting the "\r\n" delimiter after the headers, chunks can emit "\r\nSize\r\n" as a single chunk delimiter */ if (tx->length >= 0 || tx->chunkSize <= 0) { mprPutStringToBuf(buf, "\r\n"); } if (tx->altBody) { /* Error responses are emitted here */ mprPutStringToBuf(buf, tx->altBody); httpDiscardQueueData(tx->queue[HTTP_QUEUE_TX]->nextQ, 0); } tx->headerSize = mprGetBufLength(buf); tx->flags |= HTTP_TX_HEADERS_CREATED; q->count += httpGetPacketLength(packet); }