Example #1
0
void db_op(struct mg_connection *nc, const struct http_message *hm,
           const struct mg_str *key, void *db, int op) {
  switch (op) {
    case API_OP_GET: op_get(nc, hm, key, db); break;
    case API_OP_SET: op_set(nc, hm, key, db); break;
    case API_OP_DEL: op_del(nc, hm, key, db); break;
    default:
      mg_printf(nc, "%s", "HTTP/1.0 501 Not Implemented\r\n"
                "Content-Length: 0\r\n\r\n");
      break;
  }
}
Example #2
0
int parse_json(uint16_t json_sz) {

    int ret = SUCCESS;
    char *cursor = json;
    char *op = NULL;
    char *line_start = NULL, *line_end = NULL, *delim = NULL;
    uint16_t remaining = json_sz;
    uint16_t line_remaining = 0;
    uint16_t consumed = 0;

    // There's nothing to do; just return.
    if (0 == json_sz) {
#ifdef DEBUG
        fprintf(stderr, "[D] parse_json | json_sz = 0x%04x; nothing to do\n", json_sz);
#endif 
        goto bail;
    }

    // For each INSN (1 per line)...
    while (NULL != (line_start = strnchr(cursor, '\n', (uint32_t)remaining))) {

#ifdef DEBUG
        fprintf(stderr, "[D] parse_json | top of loop; line_start = '%s'\n", line_start);
#endif 

        // Consume the newline.
        line_start += 1;
        remaining -= 1;

        // If there's nothing left, we've parse it all.
        if (0 == remaining) { 
#ifdef DEBUG
            fprintf(stderr, "[D] parse_json | consumed all JSON\n", remaining);
#endif
            goto bail;
        }

        // Find next newline (so we have bounds to this line).
        if (NULL == (line_end = strnchr(line_start, '\n', (uint32_t)remaining))) {
#ifdef DEBUG
            fprintf(stderr, "[E] parse_json | could not find next line ending; line_start is: '%s'\n", line_start);
#endif  
            ret = ERRNO_MALFORMED_JSON;
            goto bail;
        }
        line_remaining = line_end - line_start;
        cursor = line_start;

#ifdef DEBUG
        fprintf(stderr, "[D] parse_json | cursor = line_start = '%s'\n", cursor);
#endif

        // Decrement from global remaining now, since we'll be consuming this line.
        remaining -= line_remaining;

        ////
        // Get OP
        ////
        if (NULL == (delim = strnchr(cursor, ' ', (uint32_t)(line_remaining)))) { 
#ifdef DEBUG
            fprintf(stderr, "[E] parse_json | cannot find OP delim\n");
#endif   
            ret = ERRNO_MALFORMED_JSON;
            goto bail;
        }
        consumed = delim - cursor;
        line_remaining -= consumed;

        if (SZ_INSN_OP != consumed) {
#ifdef DEBUG
            fprintf(stderr, "[E] parse_json | invalid OP length: %d\n", consumed);
#endif
            ret = ERRNO_MALFORMED_JSON;
            goto bail;
        }

        ////
        // Dispatch OP
        ////
        op = cursor;
        cursor += SZ_INSN_OP+1;
        if (0 == memcmp("NEW", op, SZ_INSN_OP)) {
            if (SUCCESS != (ret = op_new(&cursor, &line_remaining))) {
#ifdef DEBUG
                fprintf(stderr, "[E] parse_json | NEW | non-SUCCESS from op_new()\n");
#endif         
                goto bail;       
            }
        } else if (0 == memcmp("SET", op, SZ_INSN_OP)) {
            if (SUCCESS != (ret = op_set(&cursor, &line_remaining))) {
#ifdef DEBUG
                fprintf(stderr, "[E] parse_json | NEW | non-SUCCESS from op_set()\n");
#endif         
                goto bail;       
            }
        } else if (0 == memcmp("DEL", op, SZ_INSN_OP)) {
            if (SUCCESS != (ret = op_del(&cursor, &line_remaining))) {
#ifdef DEBUG
                fprintf(stderr, "[E] parse_json | NEW | non-SUCCESS from op_del()\n");
#endif         
                goto bail;       
            }
        } else {
#ifdef DEBUG
            fprintf(stderr, "[E] parse_json | unknown OP: '%.3s'\n", cursor);
#endif
            ret = ERRNO_MALFORMED_JSON;
            goto bail;
        }
    }

bail:
    return ret;
}