00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "queue.h"
00023 #include "rock.h"
00024 #include "stream.h"
00025 #include "fab.h"
00026 #include "util.h"
00027 #include <syslog.h>
00028 #include <iostream>
00029 #include <stdlib.h>
00030 #include <sstream>
00031 #include <fstream>
00032
00033 using namespace karoo;
00034 using std::ifstream;
00035
00036 #define SURF_POLL_MESSAGE 10
00037
00750 class surf : public rock {
00751 public:
00752 class key_val : public referable
00753 {
00754 public:
00755 text key;
00756 text val;
00757 key_val(const text& key, const text& val) { this->key = key; this->val = val; }
00758 virtual ~key_val() {}
00759 };
00760 class key_vals : public referable
00761 {
00762 public:
00763 text key;
00764 vector<text> vals;
00765 key_vals(const text& key, const text& val) { this->key = key; vals.push_back(val); }
00766 void add(const text& val) { vals.push_back(val); }
00767 bool contains(const text& val) { for (vector<text>::const_iterator it = vals.begin(); it != vals.end(); ++it) { if (*it == val) return true; } return false; }
00768 virtual ~key_vals() {}
00769 };
00770 private:
00771 mutable karoo_mutex sem;
00772 text uri_scheme;
00773 int http_port;
00774 time_t http_timeout;
00775 time_t close_linger_time;
00776 time_t close_inactivity_time;
00777 stream_service* service;
00778 HTTP_factory* factory;
00779 stream_socket* sock;
00780 exeque* listenq;
00781 exeque* streamq;
00782 exeque* httpq;
00783 HTTP_handler_factory* handler_factory;
00784 khashe mime_types;
00785 khashe env;
00786 text root;
00787 text document_root;
00788 mutable khashe document_roots;
00789 mutable khashe cave_rocks;
00790 mutable khashe cave_rock_types;
00791 text ssl_certificate;
00792 text ssl_private_key;
00793 bool ssl_ans1;
00794 text surf_root;
00795 text default_document;
00796 mutable khashe default_documents;
00797 vector<text> post_response_messages;
00798
00799 public:
00800 surf(vector<text>& opts, int argc, char *argv[]);
00801 virtual ~surf();
00802 void traverse(fab* conf, nxml_data_t* p, nxml_data_t* ctx);
00803 void configure();
00804 void startup();
00805 void stop(bool block = true);
00806 text getRoot() const { return root; }
00807 text getDocumentRoot(HTTP_parsed_data* data = NULL) const;
00808 text getDocumentRoot(const text& host) const;
00809 text getSSLCertificate() const;
00810 text getSSLPrivateKey() const;
00811 text getDefaultDocument(HTTP_parsed_data* data = NULL) const;
00812 text getSurfRoot() const;
00813 text getType() const { return "surf"; }
00814 text& writePostResponseMessages(text& s);
00815 void pushResponseMessage(const text& str);
00816 bool getMimeType(const text& ext, text& type, const text& host);
00817 bool getEnv(const text& key, text& val, const text& host);
00818 HTTP_factory* getFactory() { return factory; }
00819 bool checkRockName(const text& rock_name, const text& host);
00820 bool checkRockType(const text& rock_type, const text& host);
00821 };
00822
00823 class surf_handler: public rock_datagram_message_handler
00824 {
00825 public:
00826 surf_handler(surf* parent) : rock_datagram_message_handler(parent) {}
00827 virtual ~surf_handler() {}
00828 bool handle(uint32_t msg_type, uint64_t seq, int32_t port, const text& name, rock_datagram_message* msg);
00829 };
00830
00831 class surf_HTTP_handler_factory: public HTTP_handler_factory {
00832 private:
00833 surf* parent;
00834 public:
00835 surf_HTTP_handler_factory(surf* parent) { this->parent = parent; }
00836 virtual ~surf_HTTP_handler_factory() {}
00837
00838 HTTP_handler* create_HTTP_handler(HTTP_parsed_data* data);
00839 };
00840
00841 class surf_HTTP_handler: public HTTP_handler {
00842 private:
00843 surf* parent;
00844 bool head;
00845 public:
00846 surf_HTTP_handler(HTTP_parsed_data* data, surf* parent, bool head = false);
00847 virtual ~surf_HTTP_handler();
00848
00849 bool run();
00850 };
00851
00852 class surf_HTTP_fab_handler: public HTTP_handler {
00853 public:
00854 enum active_type {
00855 ACTIVE_TYPE_PARAMETER = 0,
00856 ACTIVE_TYPE_CONDUIT,
00857 };
00858
00859 class active : public referable
00860 {
00861 public:
00862 enum active_type type;
00863 surf_HTTP_fab_handler* parent;
00864 active(surf_HTTP_fab_handler* parent, enum active_type type);
00865 virtual ~active();
00866 };
00867
00868 class active_parameter : public active
00869 {
00870 public:
00871 text key;
00872 text val;
00873 active_parameter(surf_HTTP_fab_handler* parent, const text& key, const text& val);
00874 virtual ~active_parameter();
00875 };
00876
00877 class active_conduit : public active
00878 {
00879 public:
00880 text id;
00881 text rock_name;
00882 text rock_type;
00883 text service_id;
00884 vector<surf_HTTP_fab_handler::active_parameter*> params;
00885
00886 active_conduit(surf_HTTP_fab_handler* parent, const text& id, const text& rock_name, const text& rock_type, const text& service_id);
00887 virtual ~active_conduit();
00888 };
00889
00890 private:
00891 karoo_mutex sem;
00892 surf* parent;
00893 bool head;
00894 fab* source;
00895 text url;
00896 nxml_data_t* root;
00897 nxml_t* nxml;
00898 bool first_pass;
00899 khashe conduits;
00900 khashe params;
00901 text proxy_host;
00902 bool traverseFirstPass(nxml_data_t* node, active* context);
00903 void traverseFinalPass(nxml_data_t* node);
00904 nxml_data_t* setColumnValues(nxml_data_t* node, khashe* columns);
00905 bool traverseCheckComplete(nxml_data_t* node);
00906 text traverseGetErrors(nxml_data_t* node);
00907 bool _isComplete();
00908 public:
00909 surf_HTTP_fab_handler(HTTP_parsed_data* data, surf* parent, bool head = false);
00910 virtual ~surf_HTTP_fab_handler();
00911
00912 bool run();
00913 void timedOut();
00914
00915 void setParametersSuccess(const text& conduit_id, size_t sql_count, size_t row_count);
00916 void setParametersNull(const text& conduit_id, size_t sql_count, size_t row_count);
00917 void setParametersDone(const text& conduit_id, size_t sql_count, size_t row_count);
00918 void setParameter(const text& conduit_id, size_t sql_count, size_t row_count, khashe& columns);
00919 bool isComplete();
00920 };
00921
00922 class surf_HTTP_post_handler: public HTTP_handler {
00923 private:
00924 surf* parent;
00925 text proxy_host;
00926 public:
00927 surf_HTTP_post_handler(HTTP_parsed_data* data, surf* parent);
00928 virtual ~surf_HTTP_post_handler();
00929
00930 bool run();
00931 };
00932
00933
00934 class surf_HTTP_form_data_handler: public HTTP_handler {
00935 private:
00936 surf* parent;
00937 text boundary;
00938 text proxy_host;
00939
00940 class form_data {
00941 public:
00942 text file_name;
00943 text param_name;
00944 char* data;
00945 size_t sz;
00946 form_data() { data = NULL; sz = 0; }
00947 ~form_data() {}
00948 };
00949
00950 public:
00951 surf_HTTP_form_data_handler(HTTP_parsed_data* data, surf* parent, const text& boundary);
00952 virtual ~surf_HTTP_form_data_handler();
00953
00954 virtual bool run();
00955
00956 };
00957