~pbms-core/pbms/5.11-beta

« back to all changes in this revision

Viewing changes to mybs/src/ConnectionHandler_ms.cc

  • Committer: barry_leslie
  • Date: 2008-09-12 14:22:01 UTC
  • Revision ID: barry_leslie-71db134102f081d5227c64cc38ca9d28e72a6c9f
Internal name changes from MyBS to PBMS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2007 SNAP Innovation GmbH
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
2
2
 *
3
 
 * BLOB Streaming for MySQL
 
3
 * PrimeBase Media Stream for MySQL
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or modify
6
6
 * it under the terms of the GNU General Public License as published by
33
33
#include "CSStrUtil.h"
34
34
#include "CSHTTPStream.h"
35
35
 
36
 
#include "BSConnectionHandler.h"
37
 
#include "BSNetwork.h"
38
 
#include "BSOpenTable.h"
39
 
#include "BSUtil.h"
40
 
#include "BSEngine.h"
41
 
 
42
 
#include "bs_mysql.h"
43
 
 
44
 
BSConnectionHandler::BSConnectionHandler(CSThreadList *list):
 
36
#include "ConnectionHandler_ms.h"
 
37
#include "Network_ms.h"
 
38
#include "OpenTable_ms.h"
 
39
#include "Util_ms.h"
 
40
#include "Engine_ms.h"
 
41
 
 
42
#include "ms_mysql.h"
 
43
 
 
44
MSConnectionHandler::MSConnectionHandler(CSThreadList *list):
45
45
        CSDaemon(list),
46
46
        myMySQLTHD(NULL),
47
47
        amWaitingToListen(false),
55
55
{
56
56
}
57
57
 
58
 
BSConnectionHandler::~BSConnectionHandler()
 
58
MSConnectionHandler::~MSConnectionHandler()
59
59
{
60
60
        void *thd = myMySQLTHD;
61
61
 
62
62
        myMySQLTHD = NULL;
63
63
        if (thd)
64
 
                BSEngine::closeConnection(myMySQLTHD);
 
64
                MSEngine::closeConnection(myMySQLTHD);
65
65
        close();
66
66
        if (thd)
67
 
                bs_my_destroy_thread(thd);
 
67
                ms_my_destroy_thread(thd);
68
68
}
69
69
 
70
70
 
71
 
void BSConnectionHandler::close()
 
71
void MSConnectionHandler::close()
72
72
{
73
73
        closeStream();
74
74
        freeRequestURI();
75
75
}
76
76
 
77
 
BSConnectionHandler *BSConnectionHandler::newHandler(CSThreadList *list)
 
77
MSConnectionHandler *MSConnectionHandler::newHandler(CSThreadList *list)
78
78
{
79
 
        return new BSConnectionHandler(list);
 
79
        return new MSConnectionHandler(list);
80
80
}
81
81
 
82
82
/* Return false if not connection was openned, and the thread must quit. */
83
 
bool BSConnectionHandler::openStream()
 
83
bool MSConnectionHandler::openStream()
84
84
{
85
85
        CSSocket                *sock;
86
86
        CSInputStream   *in;
87
87
        CSOutputStream  *out;
88
88
        
89
89
        enter_();
90
 
        if (!(sock = BSNetwork::openConnection(this)))
 
90
        if (!(sock = MSNetwork::openConnection(this)))
91
91
                return_(false);
92
92
        push_(sock);
93
93
        in = sock->getInputStream();
101
101
        return_(true);
102
102
}
103
103
 
104
 
int BSConnectionHandler::getHTTPStatus(int err)
 
104
int MSConnectionHandler::getHTTPStatus(int err)
105
105
{
106
106
        int code;
107
107
 
108
108
        switch (err) {
109
 
                case BS_OK:                                             code = 200; break;
110
 
                case BS_ERR_ENGINE:                             code = 500; break;
111
 
                case BS_ERR_UNKNOWN_TABLE:              code = 404; break;
112
 
                case BS_ERR_UNKNOWN_DB:                 code = 404; break;
113
 
                case BS_ERR_DATABASE_DELETED:   code = 404; break;
114
 
                case BS_ERR_NOT_FOUND:                  code = 404; break;
115
 
                case BS_ERR_REMOVING_REPO:              code - 404; break;
116
 
                case BS_ERR_TABLE_LOCKED:               code = 412; break; // Precondition Failed
117
 
                case BS_ERR_INCORRECT_URL:              code = 404; break;
118
 
                case BS_ERR_AUTH_FAILED:                code = 403; break; // Forbidden
 
109
                case MS_OK:                                             code = 200; break;
 
110
                case MS_ERR_ENGINE:                             code = 500; break;
 
111
                case MS_ERR_UNKNOWN_TABLE:              code = 404; break;
 
112
                case MS_ERR_UNKNOWN_DB:                 code = 404; break;
 
113
                case MS_ERR_DATABASE_DELETED:   code = 404; break;
 
114
                case MS_ERR_NOT_FOUND:                  code = 404; break;
 
115
                case MS_ERR_REMOVING_REPO:              code - 404; break;
 
116
                case MS_ERR_TABLE_LOCKED:               code = 412; break; // Precondition Failed
 
117
                case MS_ERR_INCORRECT_URL:              code = 404; break;
 
118
                case MS_ERR_AUTH_FAILED:                code = 403; break; // Forbidden
119
119
                default:                                                code = 500; break;
120
120
        }
121
121
        return code;
122
122
}
123
123
 
124
 
void BSConnectionHandler::writeException(const char *qualifier)
 
124
void MSConnectionHandler::writeException(const char *qualifier)
125
125
{
126
126
        int code;
127
127
 
148
148
        iOutputStream->appendBody(myException.getStackTrace());
149
149
        iOutputStream->appendBody(EXCEPTION_REPLY_STACK_TRACE_SUFFIX_TAG);
150
150
        iOutputStream->appendBody("MySQL ");
151
 
        iOutputStream->appendBody(bs_my_version());
152
 
        iOutputStream->appendBody(", MyBS ");
153
 
        iOutputStream->appendBody(bs_version());
 
151
        iOutputStream->appendBody(ms_my_version());
 
152
        iOutputStream->appendBody(", PBMS ");
 
153
        iOutputStream->appendBody(ms_version());
154
154
        iOutputStream->appendBody("<br>Copyright &#169; 2007, SNAP Innovation GmbH</font></P></BODY></HTML>");
155
155
 
156
156
        replyPending = false;
160
160
        exit_();
161
161
}
162
162
 
163
 
void BSConnectionHandler::writeException()
 
163
void MSConnectionHandler::writeException()
164
164
{
165
165
        writeException(NULL);
166
166
}
167
167
 
168
 
void BSConnectionHandler::writeResult(const char *func, const char *file, int line, int err, char *engine, MyBSResultPtr result)
 
168
void MSConnectionHandler::writeResult(const char *func, const char *file, int line, int err, char *engine, PBMSResultPtr result)
169
169
{
170
170
        int             code;
171
171
        char    buffer[CS_EXC_MESSAGE_SIZE];
185
185
                int             cnt;
186
186
                char    engine_list[400];
187
187
 
188
 
                cnt = BSEngine::getEngineList(400, engine_list);
 
188
                cnt = MSEngine::getEngineList(400, engine_list);
189
189
                if (cnt == 0)
190
190
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, "No streaming enabled engines");
191
191
                else if (cnt == 1) {
192
 
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, "Streaming engine ");
 
192
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, "Stream engine ");
193
193
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, engine_list);
194
194
                }
195
195
                else {
196
 
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, "Streaming engines ");
 
196
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, "Stream engines ");
197
197
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, engine_list);
198
198
                }
199
199
        }
203
203
        exit_();
204
204
}
205
205
 
206
 
void BSConnectionHandler::closeStream()
 
206
void MSConnectionHandler::closeStream()
207
207
{
208
208
        enter_();
209
209
        if (iOutputStream) {
225
225
        exit_();
226
226
}
227
227
 
228
 
void BSConnectionHandler::parseRequestURI()
 
228
void MSConnectionHandler::parseRequestURI()
229
229
{
230
230
        CSString        *uri = iInputStream->getRequestURI();
231
231
        u_int           pos = 0, end;
253
253
        iCondition = uri->substr(pos);
254
254
}
255
255
 
256
 
void BSConnectionHandler::freeRequestURI()
 
256
void MSConnectionHandler::freeRequestURI()
257
257
{
258
258
        if (iTableURI)
259
259
                iTableURI->release();
266
266
        iCondition = NULL;
267
267
}
268
268
 
269
 
void BSConnectionHandler::writeFile(CSString *file_path)
 
269
void MSConnectionHandler::writeFile(CSString *file_path)
270
270
{
271
271
        CSPath                  *path;
272
272
        CSFile                  *file;
306
306
/*
307
307
 * Request URI: /<database>/<table>/<column>/<column>=<value>&<column>=<value>&...
308
308
 */
309
 
void BSConnectionHandler::handleGet()
 
309
void MSConnectionHandler::handleGet()
310
310
{
311
311
        const char      *bad_url_comment = "Incorrect URL: ";
312
 
        BSOpenTable     *otab;
 
312
        MSOpenTable     *otab;
313
313
 
314
314
        enter_();
315
315
        self->myException.setErrorCode(0);
324
324
                goto bad_url;
325
325
        
326
326
        if (iColumn->length() == 0) {
327
 
                BSBlobURLRec blob;
 
327
                MSBlobURLRec blob;
328
328
 
329
 
                if (bs_parse_blob_url(&blob, iTableURI->getCString(), false)) {
330
 
                        if (blob.bu_type == BS_URL_TYPE_BLOB || blob.bu_type == BS_URL_NEW_TYPE_BLOB) {
331
 
                                otab = BSTableList::getOpenTableByID(blob.bu_database, blob.bu_tab_id, false);
 
329
                if (ms_parse_blob_url(&blob, iTableURI->getCString(), false)) {
 
330
                        if (blob.bu_type == MS_URL_TYPE_BLOB || blob.bu_type == MS_URL_NEW_TYPE_BLOB) {
 
331
                                otab = MSTableList::getOpenTableByID(blob.bu_database, blob.bu_tab_id, false);
332
332
                                frompool_(otab);
333
333
                                otab->sendRepoBlob(blob.bu_blob_id, blob.bu_auth_code, iOutputStream);
334
334
                                backtopool_(otab);
335
335
                        }
336
336
                        else {
337
 
                                BSRepoFile      *repo_file;
 
337
                                MSRepoFile      *repo_file;
338
338
 
339
 
                                if (!(otab = BSTableList::getOpenTableByStr(blob.bu_database, true))) {
 
339
                                if (!(otab = MSTableList::getOpenTableByStr(blob.bu_database, true))) {
340
340
                                        char buffer[CS_EXC_MESSAGE_SIZE];
341
341
 
342
342
                                        cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Unknown database: ");
343
343
                                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob.bu_database);
344
 
                                        CSException::throwException(CS_CONTEXT, BS_ERR_UNKNOWN_DB, buffer);
 
344
                                        CSException::throwException(CS_CONTEXT, MS_ERR_UNKNOWN_DB, buffer);
345
345
                                }
346
346
                                frompool_(otab);
347
347
                                repo_file = otab->getDB()->getRepoFileFromPool(blob.bu_tab_id, false);
364
364
        if (iCondition->length() == 0)
365
365
                goto bad_url;
366
366
 
367
 
        if (!mybs_field_ref_enabled())
368
 
                CSException::throwException(CS_CONTEXT, BS_ERR_AUTH_FAILED, "BLOB field references not permitted. Use \"set global mybs_field_references=TRUE\" or use the command line option --mybs-field-references to enable BLOB field references.");
 
367
        if (!pbms_field_ref_enabled())
 
368
                CSException::throwException(CS_CONTEXT, MS_ERR_AUTH_FAILED, "BLOB field references not permitted. Use \"set global pbms_field_references=TRUE\" or use the command line option --mybs-field-references to enable BLOB field references.");
369
369
 
370
 
        if (!(otab = BSTableList::lockTable(iTableURI)))
 
370
        if (!(otab = MSTableList::lockTable(iTableURI)))
371
371
                goto exit;
372
372
        try_(b) {
373
373
                otab->sendEngineBlob(iColumn, iCondition, iOutputStream);
374
374
                iOutputStream->flush();
375
375
        }
376
376
        finally_(b) {
377
 
                BSTableList::unlockTable(otab, self->myException.getErrorCode() != 0);
 
377
                MSTableList::unlockTable(otab, self->myException.getErrorCode() != 0);
378
378
        }
379
379
        cont_(b);
380
380
 
386
386
 
387
387
        cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, bad_url_comment);
388
388
        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, iInputStream->getRequestURI()->getCString());
389
 
        CSException::throwException(CS_CONTEXT, BS_ERR_INCORRECT_URL, buffer);
 
389
        CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
390
390
        exit_();
391
391
}
392
392
 
393
 
void BSConnectionHandler::handlePut()
 
393
void MSConnectionHandler::handlePut()
394
394
{
395
 
        BSOpenTable *otab;
 
395
        MSOpenTable *otab;
396
396
 
397
397
        enter_();
398
398
        self->myException.setErrorCode(0);
410
410
 
411
411
                cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
412
412
                cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, iInputStream->getRequestURI()->getCString());
413
 
                CSException::throwException(CS_CONTEXT, BS_ERR_INCORRECT_URL, buffer);
 
413
                CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
414
414
        }
415
415
 
416
 
        if (!(otab = BSTableList::lockTable(iTableURI)))
 
416
        if (!(otab = MSTableList::lockTable(iTableURI)))
417
417
                goto exit;
418
418
        try_(a) {
419
419
                csWord8                 blob_len;
420
 
                MyBSBlobURLRec  bh;
 
420
                PBMSBlobURLRec  bh;
421
421
                size_t                  handle_len;
422
422
 
423
423
                blob_len = iInputStream->getContentLength();
438
438
                */
439
439
        }
440
440
        finally_(a) {
441
 
                BSTableList::unlockTable(otab, self->myException.getErrorCode() != 0);
 
441
                MSTableList::unlockTable(otab, self->myException.getErrorCode() != 0);
442
442
        }
443
443
        cont_(a);
444
444
 
446
446
        exit_();
447
447
}
448
448
 
449
 
void BSConnectionHandler::serviceConnection()
 
449
void MSConnectionHandler::serviceConnection()
450
450
{
451
451
        const char      *method;
452
452
        bool            threadStarted = false;
460
460
                         * are waiting to listen!
461
461
                         */
462
462
                        threadStarted = true;
463
 
                        if (!BSNetwork::gWaitingToListen)
464
 
                                BSNetwork::startConnectionHandler();
 
463
                        if (!MSNetwork::gWaitingToListen)
 
464
                                MSNetwork::startConnectionHandler();
465
465
                }
466
466
                replyPending = true;
467
467
                if (strcmp(method, "GET") == 0)
474
474
        }
475
475
}
476
476
 
477
 
bool BSConnectionHandler::initialize()
 
477
bool MSConnectionHandler::initialize()
478
478
{
479
 
        myMySQLTHD = bs_my_create_thread();
 
479
        myMySQLTHD = ms_my_create_thread();
480
480
        return true;
481
481
}
482
482
 
483
483
/*
484
484
 * Return false if no connection this thread should quit!
485
485
 */
486
 
bool BSConnectionHandler::doWork()
 
486
bool MSConnectionHandler::doWork()
487
487
{
488
488
        enter_();
489
489
 
502
502
        return_(false);
503
503
}
504
504
 
505
 
void *BSConnectionHandler::finalize()
 
505
void *MSConnectionHandler::finalize()
506
506
{
507
507
        void *thd;
508
508
 
512
512
        /* Free the MySQL thread: */
513
513
        if ((thd = myMySQLTHD)) {
514
514
                myMySQLTHD = NULL;
515
 
                BSEngine::closeConnection(thd);
516
 
                bs_my_destroy_thread(thd);
 
515
                MSEngine::closeConnection(thd);
 
516
                ms_my_destroy_thread(thd);
517
517
        }
518
518
        return NULL;
519
519
}
520
520
 
521
 
bool BSConnectionHandler::handleException()
 
521
bool MSConnectionHandler::handleException()
522
522
{
523
523
        if (!myMustQuit) {
524
524
                /* Start another handler if required: */
525
 
                if (!BSNetwork::gWaitingToListen)
526
 
                        BSNetwork::startConnectionHandler();
 
525
                if (!MSNetwork::gWaitingToListen)
 
526
                        MSNetwork::startConnectionHandler();
527
527
        }
528
528
        close();
529
529
        CSDaemon::handleException();