123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #include "HttpRequest.h"
- #include <iostream>
- #include <Zeit.h>
- #include "Klient.h"
- using namespace Framework;
- using namespace Network;
- using namespace HTTP;
- PostRequest::PostRequest(const char* path,
- const char* host,
- const char* data,
- const char* contentType,
- unsigned short port,
- bool useSSL)
- : ReferenceCounter()
- {
- this->path = path;
- this->host = host;
- this->data = data;
- this->contentType = contentType;
- this->port = port;
- this->useSSL = useSSL;
- }
- Answer* PostRequest::execute() const
- {
- Text message = "POST ";
- message += path;
- message += " HTTP/1.1\r\n";
- message += "Host: ";
- message += host;
- message += "\r\nContent-Type: ";
- message += contentType;
- message += "; charset=latin-1\r\nContent-Length: ";
- message += data.getLength();
- message += "\r\n\r\n";
- message += data;
- SSLKlient httpsK;
- Klient httpK;
- if (useSSL)
- {
- if (!httpsK.verbinde(port, host)) return 0;
- httpsK.sende(message, message.getLength());
- }
- else
- {
- if (!httpK.verbinde(port, host)) return 0;
- httpK.sende(message, message.getLength());
- }
- int length = -1;
- bool lastn = 0;
- Text answer;
- do
- {
- char buff[2];
- buff[1] = 0;
- if ((useSSL && httpsK.getNachricht(buff, 1))
- || (!useSSL && httpK.getNachricht(buff, 1)))
- {
- answer += buff;
- if (buff[0] == '\n')
- {
- if (length <= 0)
- {
- int p = answer.positionVon("Content-Length: ");
- if (p >= 0)
- {
- p += 16;
- Text* len = answer.getTeilText(p);
- len->remove(len->positionVon("\n"), len->getLength());
- len->remove("\r");
- length = (int)*len;
- len->release();
- }
- }
- else if (lastn)
- { // header der antwort ist vorbei
- char* buff = new char[length + 1];
- buff[length] = 0;
- if (length > 0)
- {
- if (useSSL)
- httpsK.getNachricht(buff, length);
- else
- httpK.getNachricht(buff, length);
- }
- answer += buff;
- delete[] buff;
- break;
- }
- lastn = 1;
- }
- else if (buff[0] != '\r')
- lastn = 0;
- }
- else
- break;
- } while ((useSSL && httpsK.hatNachricht(1000))
- || (!useSSL && httpK.hatNachricht(1000)));
- if (answer.getLength()) return new Answer(answer);
- return 0;
- }
- Answer::Answer(const char* answer)
- : ReferenceCounter()
- {
- all = answer;
- TextReader reader(new Text(answer));
- // parse header
- Text* line = reader.leseZeile();
- line->remove("\r\n");
- line->remove("\n");
- header += *line;
- header += "\n";
- int trenn = line->positionVon(" ");
- Text* tmp = line->getTeilText(0, trenn);
- this->protocol = *tmp;
- tmp->release();
- line->remove(0, trenn + 1);
- trenn = line->positionVon(" ");
- tmp = line->getTeilText(0, trenn);
- this->statusNumber = (int)*tmp;
- tmp->release();
- line->remove(0, trenn + 1);
- this->statusText = *line;
- line->release();
- while (!reader.istEnde())
- {
- Text* line = reader.leseZeile();
- line->remove("\r\n");
- line->remove("\n");
- if (line->getLength() > 0)
- {
- header += *line;
- header += "\n";
- }
- if (line->positionVon("Date:") == 0) this->date = line->getText() + 6;
- if (line->positionVon("Content-Type:") == 0)
- this->contentType = line->getText() + 14;
- int len = line->getLength();
- line->release();
- if (len == 0) break;
- }
- // parse body
- if (!reader.istEnde()) this->data = answer + reader.getLPosition();
- }
- const char* Answer::getContentType() const
- {
- return contentType;
- }
- const char* Answer::getData() const
- {
- return data;
- }
- int Answer::getStatusCode() const
- {
- return statusNumber;
- }
- const char* Answer::getStatusText() const
- {
- return statusText;
- }
- const char* Answer::getDate() const
- {
- return date;
- }
- const char* Answer::getAll() const
- {
- return all;
- }
|