00001
00002
00003
00004
00005
00006
00007
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <stdint.h>
00011 #include <string.h>
00012 #include <iostream>
00013 #include <assert.h>
00014
00015 #include "HttpOdb.h"
00016
00017 HttpOdb::HttpOdb(const char* url)
00018 {
00019 fDebug = 0;
00020 fUrl = strdup(url);
00021 }
00022
00023 HttpOdb::~HttpOdb()
00024 {
00025 if (fUrl)
00026 free((void*)fUrl);
00027 fUrl = NULL;
00028 }
00029
00030
00031
00032 #include "TUrl.h"
00033 #include "TSocket.h"
00034
00035 const char* HttpOdb::jget(const char* path, int index)
00036 {
00037
00038 if (index < 0)
00039 return NULL;
00040
00041 TUrl xurl(fUrl);
00042
00043 TSocket s(xurl.GetHost(), xurl.GetPort());
00044
00045
00046
00047
00048 sprintf(fRequestBuf,"GET /%s?cmd=jget&odb=%s[%d] HTTP/1.1\r\n\r\n", xurl.GetFileAndOptions(), path, index);
00049
00050 if (fDebug)
00051 printf("Sending [%s]\n", fRequestBuf);
00052
00053 s.SendRaw(fRequestBuf, strlen(fRequestBuf));
00054 int rd = s.RecvRaw(fReplyBuf, sizeof(fReplyBuf));
00055
00056 if (fDebug)
00057 printf("Received %d [%s]\n", rd, fReplyBuf);
00058
00059 if (rd < 10)
00060 return NULL;
00061
00062 if (strstr(fReplyBuf, "200 Document follows") == NULL)
00063 return NULL;
00064
00065 fReplyBuf[rd] = 0;
00066
00067 const char* p = strstr(fReplyBuf,"\r\n\r\n");
00068 if (!p)
00069 return NULL;
00070
00071 p += 4;
00072
00073 if (strcmp(p, "<DB_NO_KEY>") == 0)
00074 return NULL;
00075
00076 if (strcmp(p, "<DB_OUT_OF_RANGE>") == 0)
00077 return NULL;
00078
00079 if (strcmp(p, "<unknown>") == 0)
00080 return NULL;
00081
00082 if (strstr(p, "<html>") != NULL)
00083 {
00084 fprintf(stderr, "HttpOdb::jget: Bad mhttpd response: %s\n", p);
00085 return NULL;
00086 }
00087
00088 if (fDebug)
00089 printf("----> mhttpd %s[%d] return [%s]\n", path, index, p);
00090
00091 return p;
00092 }
00093
00094 const char* HttpOdb::jkey(const char* path)
00095 {
00096 TUrl xurl(fUrl);
00097
00098 TSocket s(xurl.GetHost(), xurl.GetPort());
00099
00100
00101
00102
00103 sprintf(fRequestBuf,"GET /%s?cmd=jkey&odb=%s HTTP/1.1\r\n\r\n", xurl.GetFileAndOptions(), path);
00104
00105 if (fDebug)
00106 printf("Sending [%s]\n", fRequestBuf);
00107
00108 s.SendRaw(fRequestBuf, strlen(fRequestBuf));
00109 int rd = s.RecvRaw(fReplyBuf, sizeof(fReplyBuf));
00110
00111 if (fDebug)
00112 printf("Received %d [%s]\n", rd, fReplyBuf);
00113
00114 if (rd < 10)
00115 return NULL;
00116
00117 if (strstr(fReplyBuf, "200 Document follows") == NULL)
00118 return NULL;
00119
00120 fReplyBuf[rd] = 0;
00121
00122 const char* p = strstr(fReplyBuf,"\r\n\r\n");
00123 if (!p)
00124 return NULL;
00125
00126 p += 4;
00127
00128 if (strcmp(p, "<DB_NO_KEY>") == 0)
00129 return NULL;
00130
00131 if (strcmp(p, "<DB_OUT_OF_RANGE>") == 0)
00132 return NULL;
00133
00134 if (strcmp(p, "<unknown>") == 0)
00135 return NULL;
00136
00137 if (strstr(p, "<html>") != NULL)
00138 {
00139 fprintf(stderr, "HttpOdb::jget: Bad mhttpd response: %s\n", p);
00140 return NULL;
00141 }
00142
00143 if (fDebug)
00144 printf("----> mhttpd %s return [%s]\n", path, p);
00145
00146 return p;
00147 }
00148
00149 int HttpOdb::odbReadAny( const char*name, int index, int tid,void* buf, int bufsize) { assert(!"Not implemented!"); }
00150
00151 uint32_t HttpOdb::odbReadUint32(const char*name, int index, uint32_t defaultValue)
00152 {
00153 const char* reply = jget(name, index);
00154 if (!reply)
00155 return defaultValue;
00156 return strtoul(reply, NULL, 0);
00157 }
00158
00159 float HttpOdb::odbReadFloat(const char*name, int index, float defaultValue)
00160 {
00161 const char* reply = jget(name, index);
00162 if (!reply)
00163 return defaultValue;
00164 return atof(reply);
00165 }
00166
00167 double HttpOdb::odbReadDouble(const char*name, int index, double defaultValue)
00168 {
00169 const char* reply = jget(name, index);
00170 if (!reply)
00171 return defaultValue;
00172 return atof(reply);
00173 }
00174
00175 int HttpOdb::odbReadInt( const char*name, int index, int defaultValue)
00176 {
00177 const char* reply = jget(name, index);
00178 if (!reply)
00179 return defaultValue;
00180 return atoi(reply);
00181 }
00182
00183 bool HttpOdb::odbReadBool( const char*name, int index, bool defaultValue)
00184 {
00185 const char* reply = jget(name, index);
00186 if (!reply)
00187 return defaultValue;
00188 if (*reply == 'n')
00189 return false;
00190 return true;
00191 }
00192
00193 const char* HttpOdb::odbReadString(const char* name, int index, const char* defaultValue)
00194 {
00195 const char* reply = jget(name, index);
00196 if (!reply)
00197 return defaultValue;
00198 return reply;
00199 }
00200
00201 int HttpOdb::odbReadArraySize(const char*name)
00202 {
00203 const char* reply = jkey(name);
00204 if (!reply)
00205 return 0;
00206 const char* p = strstr(reply, "\n");
00207 if (p)
00208 p = strstr(p+1, "\n");
00209 if (p)
00210 return atoi(p);
00211 return 1;
00212 }
00213
00214