liquid_feedback_frontend
view fastpath/getpic.c @ 979:364bd0912a15
Moved suggestion anchor to title
| author | bsw | 
|---|---|
| date | Sun Mar 10 19:28:11 2013 +0100 (2013-03-10) | 
| parents | 2dfa6f5b7670 | 
| children | 7f7142e949ee | 
 line source
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <libpq-fe.h>
     4 #include <string.h>
     5 #include <regex.h>
     6 #include <sys/types.h>
     7 #include <sys/stat.h>
     8 #include <unistd.h>
    10 #ifndef GETPIC_CONNINFO
    11 #define GETPIC_CONNINFO "dbname=liquid_feedback"
    12 #endif
    14 #ifndef GETPIC_DEFAULT_AVATAR
    15 #define GETPIC_DEFAULT_AVATAR "/opt/liquid_feedback_testing/app/static/avatar.jpg"
    16 #endif
    18 int main(int argc, const char * const *argv) {
    20   char *args_string;
    21   char *member_id;
    22   char *image_type;
    23   const char *sql_member_image_params[2];
    25 #ifndef PUBLIC_ACCESS
    26   char *cookies;
    27   regex_t session_ident_regex;
    28   ssize_t start, length;
    29   regmatch_t session_ident_regmatch[3];
    30   char *session_ident;
    31   const char *sql_session_params[1];
    32 #endif
    34   PGconn *conn;
    35   PGresult *dbr;
    37   args_string = getenv("QUERY_STRING");
    38 #ifdef PUBLIC_ACCESS
    39   if (!args_string) {
    40     fputs("Status: 403 Access Denied\n\n", stdout);
    41     return 0;
    42   }
    43 #else
    44   cookies = getenv("HTTP_COOKIE");
    45   if (!args_string || !cookies) {
    46     fputs("Status: 403 Access Denied\n\n", stdout);
    47     return 0;
    48   }
    49 #endif
    51   member_id   = strtok(args_string, "+");
    52   image_type  = strtok(NULL, "+");
    53   sql_member_image_params[0] = member_id;
    54   sql_member_image_params[1] = image_type;
    56 #ifndef PUBLIC_ACCESS
    57   if (regcomp(&session_ident_regex, "(^|[; \t])liquid_feedback_session=([0-9A-Za-z]+)", REG_EXTENDED) != 0) {
    58     // shouldn't happen
    59     abort();
    60   }
    61   if (regexec(&session_ident_regex, cookies, 3, session_ident_regmatch, 0) != 0) {
    62     fputs("Status: 403 Access Denied\n\n", stdout);
    63     return 0;
    64   }
    65   start = session_ident_regmatch[2].rm_so;
    66   length = session_ident_regmatch[2].rm_eo - session_ident_regmatch[2].rm_so;
    67   session_ident = malloc(length + 1);
    68   if (!session_ident) abort();  // shouldn't happen
    69   strncpy(session_ident, cookies + start, length);
    70   session_ident[length] = 0;
    71   sql_session_params[0] = session_ident;
    72 #endif
    74   conn = PQconnectdb(GETPIC_CONNINFO);
    75   if (!conn) {
    76     fputs("Could not create PGconn structure.\n", stderr);
    77     return 1;
    78   }
    79   if (PQstatus(conn) != CONNECTION_OK) {
    80     fputs(PQerrorMessage(conn), stderr);
    81     PQfinish(conn);
    82     return 1;
    83   }
    85 #ifndef PUBLIC_ACCESS
    86   dbr = PQexecParams(conn,
    87     "SELECT NULL FROM session JOIN member ON member.id = session.member_id WHERE session.ident = $1 AND member.active",
    88     1, NULL, sql_session_params, NULL, NULL, 0
    89   );
    90   if (PQresultStatus(dbr) != PGRES_TUPLES_OK) {
    91     fputs(PQresultErrorMessage(dbr), stderr);
    92     PQfinish(conn);
    93     return 1;
    94   }
    95   if (PQntuples(dbr) != 1) {
    96     fputs("Status: 403 Access Denied\n\n", stdout);
    97     PQfinish(conn);
    98     return 0;
    99   }
   100 #endif
   102   dbr = PQexecParams(conn,
   103     "SELECT content_type, data "
   104     "FROM member_image "
   105     "WHERE member_id = $1 "
   106     "AND image_type = $2 "
   107     "AND scaled "
   108     "LIMIT 1;",
   109     2, NULL, sql_member_image_params, NULL, NULL, 1
   110   );
   111   if (PQresultStatus(dbr) != PGRES_TUPLES_OK) {
   112     fputs(PQresultErrorMessage(dbr), stderr);
   113     PQfinish(conn);
   114     return 1;
   115   }
   116   if (PQntuples(dbr) == 0) {
   117     struct stat sb;
   118     PQclear(dbr);
   119     PQfinish(conn);
   120     fputs("Content-Type: image/jpeg\n\n", stdout);
   121     if (stat(GETPIC_DEFAULT_AVATAR, &sb)) return 1;
   122     fprintf(stdout, "Content-Length: %i\n", (int)sb.st_size);
   123     execl("/bin/cat", "cat", GETPIC_DEFAULT_AVATAR, NULL);
   124     return 1;
   125   } else {
   126     if (PQnfields(dbr) < 0) {
   127       fputs("Too few columns returned by database.\n", stderr);
   128       PQfinish(conn);
   129       return 1;
   130     }
   131     if (PQfformat(dbr, 0) != 1 || PQfformat(dbr, 1) != 1) {
   132       fputs("Database did not return data in binary format.\n", stderr);
   133       PQfinish(conn);
   134       return 1;
   135     }
   136     if (PQgetisnull(dbr, 0, 0) || PQgetisnull(dbr, 0, 1)) {
   137       fputs("Unexpected NULL in database result.\n", stderr);
   138       PQfinish(conn);
   139       return 1;
   140     }
   141     fprintf(stdout, "Content-Type: %s\n\n", PQgetvalue(dbr, 0, 0));
   142     fwrite(PQgetvalue(dbr, 0, 1), PQgetlength(dbr, 0, 1), 1, stdout);
   143   }
   144   PQfinish(conn);
   145   return 0;
   147 }
