Ignore:
Timestamp:
01/10/11 22:10:52 (10 years ago)
Author:
Admin
Message:

WebIf: Lots of further cleanups, make WebIf more responsive: static pages (CSS and JS) should now be cached by browser for 2 hours (if browser respects cache-control header).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/oscam-http-helpers.c

    r4388 r4398  
    295295  unsigned char md5tmp[MD5_DIGEST_LENGTH];
    296296  sprintf(noncetmp, "%d:%s", (int)time(NULL)/AUTHNONCEVALIDSECS, noncekey);
    297   char_to_hex(MD5((unsigned char*)noncetmp, strlen(noncetmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)result, hex2ascii);
     297  char_to_hex(MD5((unsigned char*)noncetmp, strlen(noncetmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)result);
    298298}
    299299
     
    345345        unsigned char md5tmp[MD5_DIGEST_LENGTH];
    346346        sprintf(A1tmp, "%s:%s:%s", username, AUTHREALM, expectedPassword);
    347         char_to_hex(MD5((unsigned char*)A1tmp, strlen(A1tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A1, hex2ascii);
     347        char_to_hex(MD5((unsigned char*)A1tmp, strlen(A1tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A1);
    348348       
    349349        char A2tmp[2 + strlen(method) + strlen(uri)];
    350350        sprintf(A2tmp, "%s:%s", method, uri);       
    351         char_to_hex(MD5((unsigned char*)A2tmp, strlen(A2tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A2, hex2ascii);
     351        char_to_hex(MD5((unsigned char*)A2tmp, strlen(A2tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A2);
    352352       
    353353        char A3tmp[10 + strlen(A1) + strlen(A2) + strlen(authnonce) + strlen(authnc) + strlen(authcnonce)];
    354354        sprintf(A3tmp, "%s:%s:%s:%s:auth:%s", A1, authnonce, authnc, authcnonce, A2);
    355         char_to_hex(MD5((unsigned char*)A3tmp, strlen(A3tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A3, hex2ascii);
     355        char_to_hex(MD5((unsigned char*)A3tmp, strlen(A3tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A3);
    356356       
    357357        if(strcmp(A3, authresponse) == 0) {
     
    387387}
    388388
    389 void send_headers(FILE *f, int status, char *title, char *extra, char *mime){
    390 
     389void send_headers(FILE *f, int status, char *title, char *extra, char *mime, int cache){
    391390  time_t now;
    392391  char timebuf[32];
     
    407406        pos += sprintf(pos, "Content-Type: %s\r\n", mime);
    408407
    409     pos += sprintf(pos, "Cache-Control: no-store, no-cache, must-revalidate\r\n");
    410     pos += sprintf(pos, "Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n");
     408    if(!cache){
     409        pos += sprintf(pos, "Cache-Control: no-store, no-cache, must-revalidate\r\n");
     410        pos += sprintf(pos, "Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n");
     411    } else {
     412        pos += sprintf(pos, "Cache-Control: public, max-age=7200");
     413    }
    411414    pos += sprintf(pos, "Last-Modified: %s\r\n", timebuf);
    412415    pos += sprintf(pos, "Connection: close\r\n");
     
    452455    char buf[(2* strlen(title)) + strlen(text) + 128];
    453456    char *pos = buf;
    454     send_headers(f, status, title, extra, "text/html");
     457    send_headers(f, status, title, extra, "text/html", 0);
    455458    pos += sprintf(pos, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\r\n", status, title);
    456459    pos += sprintf(pos, "<BODY><H4>%d %s</H4>\r\n", status, title);
     
    472475}
    473476
    474 char *getParamDef(struct uriparams *params, char *name, char* def){
    475     int i;
    476     for(i=(*params).paramcount-1; i>=0; --i){
    477         if(strcmp((*params).params[i], name) == 0) return (*params).values[i];
    478     }
    479     return def;
     477/* Helper function for urldecode.*/
     478int x2i(int i){
     479    i=toupper(i);
     480    i = i - '0';
     481    if(i > 9) i = i - 'A' + '9' + 1;
     482    return i;
     483}
     484
     485/* Decodes values in a http url. Note: The original value is modified! */
     486void urldecode(char *s){
     487    int c, c1, n;
     488    char *s0,*t;
     489    t = s0 = s;
     490    n = strlen(s);
     491    while(n >0){
     492        c = *s++;
     493        if(c == '+') c = ' ';
     494        else if(c == '%' && n > 2){
     495            c = *s++;
     496            c1 = c;
     497            c = *s++;
     498            c = 16*x2i(c1) + x2i(c);
     499            n -= 2;
     500        }
     501        *t++ = c;
     502        n--;
     503    }
     504    *t = 0;
     505}
     506
     507/* Encode values in a http url. Do not call free() or realloc on the returned reference or you will get memory corruption! */
     508char *urlencode(struct templatevars *vars, char *str){
     509    char buf[strlen(str) * 3 + 1];
     510    char *pstr = str, *pbuf = buf;
     511    while (*pstr) {
     512        if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') *pbuf++ = *pstr;
     513        else if (*pstr == ' ') *pbuf++ = '+';
     514        else {
     515            *pbuf++ = '%';
     516            *pbuf++ = to_hex(*pstr >> 4);
     517            *pbuf++ = to_hex(*pstr & 15);
     518        }
     519        ++pstr;
     520    }
     521    *pbuf = '\0';
     522    /* Allocate the needed memory size and store it in the templatevars */
     523    if(!cs_malloc(&pbuf, strlen(buf) + 1, -1)) return "";
     524    memcpy(pbuf, buf, strlen(buf) + 1);
     525    return tpl_addTmp(vars, pbuf);
    480526}
    481527
Note: See TracChangeset for help on using the changeset viewer.