Use this library to upload images to twitpic.com. The library creates and uses its own TCPSocket. Requires EthernetNetIf and DNSResolver. (this library actually designed by a friend of mine, novachild, with no mbed license of his own.)

Dependents:   TwitPicExample

Committer:
treebykooba
Date:
Sat Feb 26 20:07:01 2011 +0000
Revision:
0:e8e33b39c3cc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
treebykooba 0:e8e33b39c3cc 1 /*
treebykooba 0:e8e33b39c3cc 2 TwitPic.cpp - TwitPic Image Uploader for mbed
treebykooba 0:e8e33b39c3cc 3 Copyright (c) novachild 2011. All right reserved.
treebykooba 0:e8e33b39c3cc 4
treebykooba 0:e8e33b39c3cc 5 This library is distributed in the hope that it will be useful,
treebykooba 0:e8e33b39c3cc 6 but WITHOUT ANY WARRANTY; without even the implied warranty of
treebykooba 0:e8e33b39c3cc 7 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
treebykooba 0:e8e33b39c3cc 8 */
treebykooba 0:e8e33b39c3cc 9
treebykooba 0:e8e33b39c3cc 10 #include "TwitPic.h"
treebykooba 0:e8e33b39c3cc 11 #include "string.h"
treebykooba 0:e8e33b39c3cc 12
treebykooba 0:e8e33b39c3cc 13 #define MIN(a, b) (a < b ? a : b)
treebykooba 0:e8e33b39c3cc 14 #define MAX(a, b) (a > b ? a : b)
treebykooba 0:e8e33b39c3cc 15
treebykooba 0:e8e33b39c3cc 16 #define SENDING_BUFFER_SIZE 4096 //must be large enough to hold all parameters: I wouldn't go under 2 KB.
treebykooba 0:e8e33b39c3cc 17 #define RECEIVING_BUFFER_SIZE 1024 //for holding respones (early ones will be discared, 1KB should do)
treebykooba 0:e8e33b39c3cc 18 #define SENDING_CHUNK_SIZE 1024 //amount to send over the socket at once.
treebykooba 0:e8e33b39c3cc 19
treebykooba 0:e8e33b39c3cc 20 #define TWITPIC_URL "api.twitpic.com"
treebykooba 0:e8e33b39c3cc 21 #define TWITPIC_UPLOAD_API_URL "/1/upload.xml"
treebykooba 0:e8e33b39c3cc 22 #define TWITPIC_UPLOAD_AND_POST_API_URL "/1/uploadAndPost.xml"
treebykooba 0:e8e33b39c3cc 23
treebykooba 0:e8e33b39c3cc 24 #define BOUNDARY "----------0dcb31395642"
treebykooba 0:e8e33b39c3cc 25 #define HEADER "--" BOUNDARY
treebykooba 0:e8e33b39c3cc 26 #define FOOTER "--" BOUNDARY "--"
treebykooba 0:e8e33b39c3cc 27
treebykooba 0:e8e33b39c3cc 28 #define DEBUG 1
treebykooba 0:e8e33b39c3cc 29 #define TRACE 1
treebykooba 0:e8e33b39c3cc 30
treebykooba 0:e8e33b39c3cc 31
treebykooba 0:e8e33b39c3cc 32 const char* m_twitpic_api_key; //twitpic API key
treebykooba 0:e8e33b39c3cc 33 const char* m_consumer_key; //Consumer key
treebykooba 0:e8e33b39c3cc 34 const char* m_consumer_secret; //Consumer secret
treebykooba 0:e8e33b39c3cc 35 const char* m_access_token; //Access Token
treebykooba 0:e8e33b39c3cc 36 const char* m_access_token_secret; //Access Token secret
treebykooba 0:e8e33b39c3cc 37 const char* twitterMessage;
treebykooba 0:e8e33b39c3cc 38
treebykooba 0:e8e33b39c3cc 39 bool post = false;
treebykooba 0:e8e33b39c3cc 40
treebykooba 0:e8e33b39c3cc 41 bool sentHeaders = false;
treebykooba 0:e8e33b39c3cc 42 bool sentParameters = false;
treebykooba 0:e8e33b39c3cc 43 bool sentImageData = false;
treebykooba 0:e8e33b39c3cc 44 bool sentFooter = false;
treebykooba 0:e8e33b39c3cc 45 bool finishedSending = false;
treebykooba 0:e8e33b39c3cc 46
treebykooba 0:e8e33b39c3cc 47 bool positiveResponse = false;
treebykooba 0:e8e33b39c3cc 48 bool readyToReturn = false;
treebykooba 0:e8e33b39c3cc 49 bool stillPolling = false;
treebykooba 0:e8e33b39c3cc 50
treebykooba 0:e8e33b39c3cc 51 char sendBuf[SENDING_BUFFER_SIZE];
treebykooba 0:e8e33b39c3cc 52 char recvBuf[RECEIVING_BUFFER_SIZE];
treebykooba 0:e8e33b39c3cc 53
treebykooba 0:e8e33b39c3cc 54 int imgSize=0;
treebykooba 0:e8e33b39c3cc 55 int writing_position = 0;
treebykooba 0:e8e33b39c3cc 56 int errorCode = 0;
treebykooba 0:e8e33b39c3cc 57 int preCalcedContentLength=0;
treebykooba 0:e8e33b39c3cc 58
treebykooba 0:e8e33b39c3cc 59 int totalSentData=0;
treebykooba 0:e8e33b39c3cc 60
treebykooba 0:e8e33b39c3cc 61 FILE *imgFile;
treebykooba 0:e8e33b39c3cc 62
treebykooba 0:e8e33b39c3cc 63
treebykooba 0:e8e33b39c3cc 64 TwitPic::TwitPic(const char *twitpic_api_key, const char *consumer_key, const char *consumer_secret, const char *access_token, const char *access_token_secret) {
treebykooba 0:e8e33b39c3cc 65
treebykooba 0:e8e33b39c3cc 66 m_twitpic_api_key = twitpic_api_key;
treebykooba 0:e8e33b39c3cc 67 m_consumer_key = consumer_key;
treebykooba 0:e8e33b39c3cc 68 m_consumer_secret = consumer_secret;
treebykooba 0:e8e33b39c3cc 69 m_access_token = access_token;
treebykooba 0:e8e33b39c3cc 70 m_access_token_secret = access_token_secret;
treebykooba 0:e8e33b39c3cc 71 socket.setOnEvent(this, &TwitPic::handleTCPSocketEvents);
treebykooba 0:e8e33b39c3cc 72
treebykooba 0:e8e33b39c3cc 73 preCalcedContentLength = 7 * (sizeof(HEADER)-1) +
treebykooba 0:e8e33b39c3cc 74 24* (sizeof("\r\n")-1) +
treebykooba 0:e8e33b39c3cc 75 6 * (sizeof("Content-Disposition: form-data; name=\"") - 1) +
treebykooba 0:e8e33b39c3cc 76 sizeof("key") +
treebykooba 0:e8e33b39c3cc 77 sizeof("consumer_token") +
treebykooba 0:e8e33b39c3cc 78 sizeof("consumer_secret") +
treebykooba 0:e8e33b39c3cc 79 sizeof("oauth_token") +
treebykooba 0:e8e33b39c3cc 80 sizeof("oauth_secret") +
treebykooba 0:e8e33b39c3cc 81 sizeof("message") +
treebykooba 0:e8e33b39c3cc 82 strlen(m_twitpic_api_key) +
treebykooba 0:e8e33b39c3cc 83 strlen(m_consumer_key) +
treebykooba 0:e8e33b39c3cc 84 strlen(m_consumer_secret) +
treebykooba 0:e8e33b39c3cc 85 strlen(m_access_token) +
treebykooba 0:e8e33b39c3cc 86 strlen(m_access_token_secret) +
treebykooba 0:e8e33b39c3cc 87 sizeof("\r\nContent-Disposition: form-data; name=\"media\"; filename=\"image.jpg\"\r\n")-1 +
treebykooba 0:e8e33b39c3cc 88 sizeof("Content-Type: image/jpeg\r\n")-1 +
treebykooba 0:e8e33b39c3cc 89 sizeof("Content-Transfer-Encoding: binary\r\n\r\n")-1 +
treebykooba 0:e8e33b39c3cc 90 sizeof("\r\n")-1 +
treebykooba 0:e8e33b39c3cc 91 sizeof(FOOTER) - 1 +
treebykooba 0:e8e33b39c3cc 92 sizeof("\r\n") - 1;
treebykooba 0:e8e33b39c3cc 93
treebykooba 0:e8e33b39c3cc 94 }
treebykooba 0:e8e33b39c3cc 95
treebykooba 0:e8e33b39c3cc 96 int TwitPic::upload(const char *message,
treebykooba 0:e8e33b39c3cc 97 FILE *img,
treebykooba 0:e8e33b39c3cc 98 bool toPost) {
treebykooba 0:e8e33b39c3cc 99
treebykooba 0:e8e33b39c3cc 100 if (TRACE) printf("-----------------------TRACE: upload\n");
treebykooba 0:e8e33b39c3cc 101 post = toPost;
treebykooba 0:e8e33b39c3cc 102 imgFile = img;
treebykooba 0:e8e33b39c3cc 103 twitterMessage = message;
treebykooba 0:e8e33b39c3cc 104
treebykooba 0:e8e33b39c3cc 105 DNSResolver resolver;
treebykooba 0:e8e33b39c3cc 106 server.setIp(resolver.resolveName("api.twitpic.com"));
treebykooba 0:e8e33b39c3cc 107 server.setPort(80);
treebykooba 0:e8e33b39c3cc 108
treebykooba 0:e8e33b39c3cc 109 //Calculate image size
treebykooba 0:e8e33b39c3cc 110 fseek (imgFile , 0 , SEEK_END);
treebykooba 0:e8e33b39c3cc 111 imgSize = ftell (img);
treebykooba 0:e8e33b39c3cc 112 rewind (imgFile);
treebykooba 0:e8e33b39c3cc 113
treebykooba 0:e8e33b39c3cc 114
treebykooba 0:e8e33b39c3cc 115
treebykooba 0:e8e33b39c3cc 116 sentHeaders = false;
treebykooba 0:e8e33b39c3cc 117 sentParameters = false;
treebykooba 0:e8e33b39c3cc 118 sentImageData = false;
treebykooba 0:e8e33b39c3cc 119 sentFooter = false;
treebykooba 0:e8e33b39c3cc 120 positiveResponse = false;
treebykooba 0:e8e33b39c3cc 121 finishedSending = false;
treebykooba 0:e8e33b39c3cc 122 readyToReturn = false;
treebykooba 0:e8e33b39c3cc 123 writing_position = 0;
treebykooba 0:e8e33b39c3cc 124
treebykooba 0:e8e33b39c3cc 125 if (DEBUG) printf("Opening connection to api.twitpic.com\n");
treebykooba 0:e8e33b39c3cc 126 TCPSocketErr err = socket.connect(server);
treebykooba 0:e8e33b39c3cc 127 if (err < 0) {
treebykooba 0:e8e33b39c3cc 128 if (DEBUG) printf("Error connecting socket: %i",err);
treebykooba 0:e8e33b39c3cc 129 return err;
treebykooba 0:e8e33b39c3cc 130 }
treebykooba 0:e8e33b39c3cc 131
treebykooba 0:e8e33b39c3cc 132 if (DEBUG) printf("Beginning network polling cycle.\n");
treebykooba 0:e8e33b39c3cc 133 int countAfterSent = 0;
treebykooba 0:e8e33b39c3cc 134 while (! readyToReturn) {
treebykooba 0:e8e33b39c3cc 135 if (TRACE && !stillPolling) printf("-----------------------TRACE: Net::poll()\n");
treebykooba 0:e8e33b39c3cc 136 stillPolling = true;
treebykooba 0:e8e33b39c3cc 137 Net::poll();
treebykooba 0:e8e33b39c3cc 138 if (finishedSending) countAfterSent++;
treebykooba 0:e8e33b39c3cc 139 if (countAfterSent > 1000000) {
treebykooba 0:e8e33b39c3cc 140 if (DEBUG) printf("Polled 1000000 times and got no response. Quitting.\n");
treebykooba 0:e8e33b39c3cc 141 positiveResponse = false;
treebykooba 0:e8e33b39c3cc 142 readyToReturn = true;
treebykooba 0:e8e33b39c3cc 143 }
treebykooba 0:e8e33b39c3cc 144 }
treebykooba 0:e8e33b39c3cc 145
treebykooba 0:e8e33b39c3cc 146 if (positiveResponse) {
treebykooba 0:e8e33b39c3cc 147 printf("Image uploaded.\n");
treebykooba 0:e8e33b39c3cc 148 return 0;
treebykooba 0:e8e33b39c3cc 149 } else {
treebykooba 0:e8e33b39c3cc 150 printf("Image not successfully uploaded\n");
treebykooba 0:e8e33b39c3cc 151 return errorCode;
treebykooba 0:e8e33b39c3cc 152 }
treebykooba 0:e8e33b39c3cc 153
treebykooba 0:e8e33b39c3cc 154 }
treebykooba 0:e8e33b39c3cc 155
treebykooba 0:e8e33b39c3cc 156 int TwitPic::uploadAndPost(const char *message,
treebykooba 0:e8e33b39c3cc 157 FILE *imgFile) {
treebykooba 0:e8e33b39c3cc 158 return upload(message, imgFile, true);
treebykooba 0:e8e33b39c3cc 159 }
treebykooba 0:e8e33b39c3cc 160
treebykooba 0:e8e33b39c3cc 161 void TwitPic::handleTCPSocketEvents(TCPSocketEvent ev) {
treebykooba 0:e8e33b39c3cc 162 stillPolling = false;
treebykooba 0:e8e33b39c3cc 163 if (TRACE) printf("-----------------------TRACE: handleTCPSocketEvent\n");
treebykooba 0:e8e33b39c3cc 164 switch (ev) {
treebykooba 0:e8e33b39c3cc 165 case TCPSOCKET_ERROR:
treebykooba 0:e8e33b39c3cc 166 printf("-----TCPSocket: Unknown Error\n");
treebykooba 0:e8e33b39c3cc 167 goto error;
treebykooba 0:e8e33b39c3cc 168 case TCPSOCKET_CONABRT:
treebykooba 0:e8e33b39c3cc 169 printf("-----TCPSocket: Connection aborted.\n");
treebykooba 0:e8e33b39c3cc 170 goto error;
treebykooba 0:e8e33b39c3cc 171 case TCPSOCKET_CONRST:
treebykooba 0:e8e33b39c3cc 172 printf("-----TCPSocket: Connection reset.\n");
treebykooba 0:e8e33b39c3cc 173 goto error;
treebykooba 0:e8e33b39c3cc 174 case TCPSOCKET_CONTIMEOUT:
treebykooba 0:e8e33b39c3cc 175 printf("-----TCPSocket: Connection timed out.\n");
treebykooba 0:e8e33b39c3cc 176 goto error;
treebykooba 0:e8e33b39c3cc 177 case TCPSOCKET_DISCONNECTED:
treebykooba 0:e8e33b39c3cc 178 printf("-----TCPSocket: Connection disconnected.\n");
treebykooba 0:e8e33b39c3cc 179 error:
treebykooba 0:e8e33b39c3cc 180 readyToReturn = true;
treebykooba 0:e8e33b39c3cc 181 errorCode = ev;
treebykooba 0:e8e33b39c3cc 182 positiveResponse = false;
treebykooba 0:e8e33b39c3cc 183 break;
treebykooba 0:e8e33b39c3cc 184 case TCPSOCKET_CONNECTED:
treebykooba 0:e8e33b39c3cc 185 if (DEBUG) printf("-----TCPSocket: Connected.\r\n");
treebykooba 0:e8e33b39c3cc 186 sendNextChunk();
treebykooba 0:e8e33b39c3cc 187 break;
treebykooba 0:e8e33b39c3cc 188 case TCPSOCKET_READABLE:
treebykooba 0:e8e33b39c3cc 189 if (DEBUG) printf("-----TCPSocket: Readable.\r\n");
treebykooba 0:e8e33b39c3cc 190 getResponse();
treebykooba 0:e8e33b39c3cc 191 break;
treebykooba 0:e8e33b39c3cc 192 case TCPSOCKET_WRITEABLE:
treebykooba 0:e8e33b39c3cc 193 if (DEBUG) printf("-----TCPSOCKET: Writeable.\r\n");
treebykooba 0:e8e33b39c3cc 194 if (writing_position < SENDING_BUFFER_SIZE) {
treebykooba 0:e8e33b39c3cc 195 if (!sentHeaders) {
treebykooba 0:e8e33b39c3cc 196 sendHeaders();
treebykooba 0:e8e33b39c3cc 197 } else if (!sentParameters) {
treebykooba 0:e8e33b39c3cc 198 sendParameters();
treebykooba 0:e8e33b39c3cc 199 } else if (!sentImageData) {
treebykooba 0:e8e33b39c3cc 200 sendImageData();
treebykooba 0:e8e33b39c3cc 201 } else if (!sentFooter) {
treebykooba 0:e8e33b39c3cc 202 sendFooter();
treebykooba 0:e8e33b39c3cc 203 }
treebykooba 0:e8e33b39c3cc 204 }
treebykooba 0:e8e33b39c3cc 205 if (writing_position > 0) sendNextChunk();
treebykooba 0:e8e33b39c3cc 206 }
treebykooba 0:e8e33b39c3cc 207 }
treebykooba 0:e8e33b39c3cc 208
treebykooba 0:e8e33b39c3cc 209 void TwitPic::sendHeaders() {
treebykooba 0:e8e33b39c3cc 210 char* positionPointer;
treebykooba 0:e8e33b39c3cc 211 char imgSizeAsString[25];
treebykooba 0:e8e33b39c3cc 212 int neededSize = 0;
treebykooba 0:e8e33b39c3cc 213 int availableSize = 0;
treebykooba 0:e8e33b39c3cc 214
treebykooba 0:e8e33b39c3cc 215 if (TRACE) printf("-----------------------TRACE: sendHeaders\n");
treebykooba 0:e8e33b39c3cc 216
treebykooba 0:e8e33b39c3cc 217 sprintf(imgSizeAsString,"%i",preCalcedContentLength + +strlen(twitterMessage) + imgSize);
treebykooba 0:e8e33b39c3cc 218
treebykooba 0:e8e33b39c3cc 219 if (post) {
treebykooba 0:e8e33b39c3cc 220 neededSize = sizeof(TWITPIC_UPLOAD_AND_POST_API_URL) -1;
treebykooba 0:e8e33b39c3cc 221 } else {
treebykooba 0:e8e33b39c3cc 222 neededSize = sizeof(TWITPIC_UPLOAD_API_URL)-1;
treebykooba 0:e8e33b39c3cc 223 }
treebykooba 0:e8e33b39c3cc 224 neededSize += sizeof("POST ") -1 +
treebykooba 0:e8e33b39c3cc 225 sizeof(" HTTP/1.0\r\n")-1 +
treebykooba 0:e8e33b39c3cc 226 sizeof("Content-Type: multipart/form-data; boundary=")-1 +
treebykooba 0:e8e33b39c3cc 227 sizeof(BOUNDARY)-1 +
treebykooba 0:e8e33b39c3cc 228 sizeof("\r\nContent-Length: ") - 1 +
treebykooba 0:e8e33b39c3cc 229 strlen(imgSizeAsString) +
treebykooba 0:e8e33b39c3cc 230 sizeof("\r\nExpect: 100-continue") -1 +
treebykooba 0:e8e33b39c3cc 231 sizeof("\r\nHost: api.twitpic.com") - 1 +
treebykooba 0:e8e33b39c3cc 232 sizeof("\r\n\r\n") - 1;
treebykooba 0:e8e33b39c3cc 233
treebykooba 0:e8e33b39c3cc 234 availableSize = SENDING_BUFFER_SIZE - writing_position;
treebykooba 0:e8e33b39c3cc 235
treebykooba 0:e8e33b39c3cc 236
treebykooba 0:e8e33b39c3cc 237 //for this guy, only add to sendBuf if you can add everything
treebykooba 0:e8e33b39c3cc 238 if (availableSize < neededSize) return;
treebykooba 0:e8e33b39c3cc 239
treebykooba 0:e8e33b39c3cc 240 positionPointer = sendBuf;
treebykooba 0:e8e33b39c3cc 241 positionPointer += writing_position;
treebykooba 0:e8e33b39c3cc 242 strcpy(positionPointer, "POST ");
treebykooba 0:e8e33b39c3cc 243 positionPointer += 5;
treebykooba 0:e8e33b39c3cc 244 if (post) {
treebykooba 0:e8e33b39c3cc 245 strcpy(positionPointer, TWITPIC_UPLOAD_AND_POST_API_URL);
treebykooba 0:e8e33b39c3cc 246 positionPointer += sizeof(TWITPIC_UPLOAD_AND_POST_API_URL)-1;
treebykooba 0:e8e33b39c3cc 247 } else {
treebykooba 0:e8e33b39c3cc 248 strcpy(positionPointer, TWITPIC_UPLOAD_API_URL);
treebykooba 0:e8e33b39c3cc 249 positionPointer += sizeof(TWITPIC_UPLOAD_API_URL)-1;
treebykooba 0:e8e33b39c3cc 250 }
treebykooba 0:e8e33b39c3cc 251 strcpy(positionPointer, " HTTP/1.1\r\n");
treebykooba 0:e8e33b39c3cc 252 positionPointer += 11;
treebykooba 0:e8e33b39c3cc 253 strcpy(positionPointer, "Host: api.twitpic.com\r\n");
treebykooba 0:e8e33b39c3cc 254 positionPointer+= sizeof("Host: api.twitpic.com\r\n")-1;
treebykooba 0:e8e33b39c3cc 255 strcpy(positionPointer, "Content-Length: ");
treebykooba 0:e8e33b39c3cc 256 positionPointer += sizeof("Content-Length: ") - 1;
treebykooba 0:e8e33b39c3cc 257 strcpy(positionPointer, imgSizeAsString);
treebykooba 0:e8e33b39c3cc 258 positionPointer += strlen(imgSizeAsString);
treebykooba 0:e8e33b39c3cc 259 strcpy(positionPointer,"\r\nExpect: 100-continue");
treebykooba 0:e8e33b39c3cc 260 positionPointer+=sizeof("\r\nExpect: 100-continue")-1;
treebykooba 0:e8e33b39c3cc 261 strcpy(positionPointer, "\r\nContent-Type: multipart/form-data; boundary=");
treebykooba 0:e8e33b39c3cc 262 positionPointer += sizeof("\r\nContent-Type: multipart/form-data; boundary=")-1;
treebykooba 0:e8e33b39c3cc 263 strcpy(positionPointer, BOUNDARY);
treebykooba 0:e8e33b39c3cc 264 positionPointer += sizeof(BOUNDARY)-1;
treebykooba 0:e8e33b39c3cc 265 strcpy(positionPointer, "\r\n\r\n");
treebykooba 0:e8e33b39c3cc 266 positionPointer += 4;
treebykooba 0:e8e33b39c3cc 267
treebykooba 0:e8e33b39c3cc 268 writing_position += neededSize;
treebykooba 0:e8e33b39c3cc 269 if (DEBUG) printf("Wrote headers to buffer. Size = %i\n. Image size reported: %s\n",neededSize, imgSizeAsString);
treebykooba 0:e8e33b39c3cc 270 sentHeaders = true;
treebykooba 0:e8e33b39c3cc 271 }
treebykooba 0:e8e33b39c3cc 272
treebykooba 0:e8e33b39c3cc 273 void TwitPic::sendParameters() {
treebykooba 0:e8e33b39c3cc 274 char* positionPointer;
treebykooba 0:e8e33b39c3cc 275 int neededSize = 0;
treebykooba 0:e8e33b39c3cc 276 int availableSize = 0;
treebykooba 0:e8e33b39c3cc 277 int actualsize = 0;
treebykooba 0:e8e33b39c3cc 278 char headerline[] = "Content-Disposition: form-data; name=\"";
treebykooba 0:e8e33b39c3cc 279
treebykooba 0:e8e33b39c3cc 280 if (TRACE) printf("-----------------------TRACE: sendParameters\n");
treebykooba 0:e8e33b39c3cc 281 neededSize = 7 * (sizeof(HEADER)-1) +
treebykooba 0:e8e33b39c3cc 282 24* (sizeof("\r\n")-1) +
treebykooba 0:e8e33b39c3cc 283 6 * (sizeof(headerline) - 1) +
treebykooba 0:e8e33b39c3cc 284 sizeof("key") +
treebykooba 0:e8e33b39c3cc 285 sizeof("consumer_token") +
treebykooba 0:e8e33b39c3cc 286 sizeof("consumer_secret") +
treebykooba 0:e8e33b39c3cc 287 sizeof("oauth_token") +
treebykooba 0:e8e33b39c3cc 288 sizeof("oauth_secret") +
treebykooba 0:e8e33b39c3cc 289 sizeof("message") +
treebykooba 0:e8e33b39c3cc 290 strlen(m_twitpic_api_key) +
treebykooba 0:e8e33b39c3cc 291 strlen(m_consumer_key) +
treebykooba 0:e8e33b39c3cc 292 strlen(m_consumer_secret) +
treebykooba 0:e8e33b39c3cc 293 strlen(m_access_token) +
treebykooba 0:e8e33b39c3cc 294 strlen(m_access_token_secret) +
treebykooba 0:e8e33b39c3cc 295 strlen(twitterMessage) +
treebykooba 0:e8e33b39c3cc 296 sizeof("\r\nContent-Disposition: form-data; name=\"media\"; filename=\"image.jpg\"\r\n")-1 +
treebykooba 0:e8e33b39c3cc 297 sizeof("Content-Type: image/jpeg\r\n")-1 +
treebykooba 0:e8e33b39c3cc 298 sizeof("Content-Transfer-Encoding: binary\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 299
treebykooba 0:e8e33b39c3cc 300
treebykooba 0:e8e33b39c3cc 301 availableSize = SENDING_BUFFER_SIZE - writing_position;
treebykooba 0:e8e33b39c3cc 302
treebykooba 0:e8e33b39c3cc 303
treebykooba 0:e8e33b39c3cc 304 //for this guy, only add to sendBuf if you can add everything
treebykooba 0:e8e33b39c3cc 305 if (availableSize < neededSize) return;
treebykooba 0:e8e33b39c3cc 306
treebykooba 0:e8e33b39c3cc 307 positionPointer = sendBuf;
treebykooba 0:e8e33b39c3cc 308 positionPointer+=writing_position;
treebykooba 0:e8e33b39c3cc 309
treebykooba 0:e8e33b39c3cc 310 //API Key
treebykooba 0:e8e33b39c3cc 311 strcpy(positionPointer, HEADER);
treebykooba 0:e8e33b39c3cc 312 positionPointer += sizeof(HEADER)-1;
treebykooba 0:e8e33b39c3cc 313 actualsize += sizeof(HEADER) -1;
treebykooba 0:e8e33b39c3cc 314 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 315 positionPointer += 2;
treebykooba 0:e8e33b39c3cc 316 actualsize += 2;
treebykooba 0:e8e33b39c3cc 317
treebykooba 0:e8e33b39c3cc 318 strcpy(positionPointer, headerline);
treebykooba 0:e8e33b39c3cc 319 positionPointer += strlen(headerline);
treebykooba 0:e8e33b39c3cc 320 actualsize += strlen(headerline);
treebykooba 0:e8e33b39c3cc 321 strcpy(positionPointer, "key\"\r\n\r\n");
treebykooba 0:e8e33b39c3cc 322 positionPointer += sizeof("key\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 323 actualsize += sizeof("key\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 324 strcpy(positionPointer, m_twitpic_api_key);
treebykooba 0:e8e33b39c3cc 325 positionPointer += strlen(m_twitpic_api_key);
treebykooba 0:e8e33b39c3cc 326 actualsize +=strlen(m_twitpic_api_key);
treebykooba 0:e8e33b39c3cc 327 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 328 positionPointer+=2;
treebykooba 0:e8e33b39c3cc 329 actualsize +=2;
treebykooba 0:e8e33b39c3cc 330 //Consumer Key
treebykooba 0:e8e33b39c3cc 331 strcpy(positionPointer, HEADER);
treebykooba 0:e8e33b39c3cc 332 positionPointer += sizeof(HEADER)-1;
treebykooba 0:e8e33b39c3cc 333 actualsize += sizeof(HEADER) -1;
treebykooba 0:e8e33b39c3cc 334 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 335 positionPointer += 2;
treebykooba 0:e8e33b39c3cc 336 actualsize += 2;
treebykooba 0:e8e33b39c3cc 337
treebykooba 0:e8e33b39c3cc 338 strcpy(positionPointer, headerline);
treebykooba 0:e8e33b39c3cc 339 positionPointer += strlen(headerline);
treebykooba 0:e8e33b39c3cc 340 actualsize += strlen(headerline);
treebykooba 0:e8e33b39c3cc 341 strcpy(positionPointer, "consumer_token\"\r\n\r\n");
treebykooba 0:e8e33b39c3cc 342 positionPointer += sizeof("consumer_token\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 343 actualsize += sizeof("consumer_token\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 344 strcpy(positionPointer, m_consumer_key);
treebykooba 0:e8e33b39c3cc 345 positionPointer += strlen(m_consumer_key);
treebykooba 0:e8e33b39c3cc 346 actualsize += strlen(m_consumer_key);
treebykooba 0:e8e33b39c3cc 347 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 348 positionPointer+=2;
treebykooba 0:e8e33b39c3cc 349 actualsize+=2;
treebykooba 0:e8e33b39c3cc 350
treebykooba 0:e8e33b39c3cc 351 //Consumer Secret
treebykooba 0:e8e33b39c3cc 352 strcpy(positionPointer, HEADER);
treebykooba 0:e8e33b39c3cc 353 positionPointer += sizeof(HEADER)-1;
treebykooba 0:e8e33b39c3cc 354 actualsize += sizeof(HEADER) -1;
treebykooba 0:e8e33b39c3cc 355 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 356 positionPointer += 2;
treebykooba 0:e8e33b39c3cc 357 actualsize += 2;
treebykooba 0:e8e33b39c3cc 358
treebykooba 0:e8e33b39c3cc 359 strcpy(positionPointer, headerline);
treebykooba 0:e8e33b39c3cc 360 positionPointer += strlen(headerline);
treebykooba 0:e8e33b39c3cc 361 actualsize += strlen(headerline);
treebykooba 0:e8e33b39c3cc 362 strcpy(positionPointer, "consumer_secret\"\r\n\r\n");
treebykooba 0:e8e33b39c3cc 363 positionPointer += sizeof("consumer_secret\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 364 actualsize +=sizeof("consumer_secret\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 365 strcpy(positionPointer, m_consumer_secret);
treebykooba 0:e8e33b39c3cc 366 positionPointer += strlen(m_consumer_secret);
treebykooba 0:e8e33b39c3cc 367 actualsize += strlen(m_consumer_secret);
treebykooba 0:e8e33b39c3cc 368 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 369 positionPointer+=2;
treebykooba 0:e8e33b39c3cc 370 actualsize +=2;
treebykooba 0:e8e33b39c3cc 371
treebykooba 0:e8e33b39c3cc 372 //OAuth Token
treebykooba 0:e8e33b39c3cc 373 strcpy(positionPointer, HEADER);
treebykooba 0:e8e33b39c3cc 374 positionPointer += sizeof(HEADER)-1;
treebykooba 0:e8e33b39c3cc 375 actualsize += sizeof(HEADER) -1;
treebykooba 0:e8e33b39c3cc 376 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 377 positionPointer += 2;
treebykooba 0:e8e33b39c3cc 378 actualsize += 2;
treebykooba 0:e8e33b39c3cc 379
treebykooba 0:e8e33b39c3cc 380 strcpy(positionPointer, headerline);
treebykooba 0:e8e33b39c3cc 381 positionPointer += strlen(headerline);
treebykooba 0:e8e33b39c3cc 382 actualsize += strlen(headerline);
treebykooba 0:e8e33b39c3cc 383 strcpy(positionPointer, "oauth_token\"\r\n\r\n");
treebykooba 0:e8e33b39c3cc 384 positionPointer += sizeof("oauth_token\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 385 actualsize += sizeof("oauth_token\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 386 strcpy(positionPointer, m_access_token);
treebykooba 0:e8e33b39c3cc 387 positionPointer += strlen(m_access_token);
treebykooba 0:e8e33b39c3cc 388 actualsize += strlen(m_access_token);
treebykooba 0:e8e33b39c3cc 389 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 390 positionPointer+=2;
treebykooba 0:e8e33b39c3cc 391 actualsize += 2;
treebykooba 0:e8e33b39c3cc 392
treebykooba 0:e8e33b39c3cc 393 //OAuth Secret
treebykooba 0:e8e33b39c3cc 394 strcpy(positionPointer, HEADER);
treebykooba 0:e8e33b39c3cc 395 positionPointer += sizeof(HEADER)-1;
treebykooba 0:e8e33b39c3cc 396 actualsize += sizeof(HEADER) -1;
treebykooba 0:e8e33b39c3cc 397 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 398 positionPointer += 2;
treebykooba 0:e8e33b39c3cc 399 actualsize += 2;
treebykooba 0:e8e33b39c3cc 400
treebykooba 0:e8e33b39c3cc 401 strcpy(positionPointer, headerline);
treebykooba 0:e8e33b39c3cc 402 positionPointer += strlen(headerline);
treebykooba 0:e8e33b39c3cc 403 actualsize += strlen(headerline);
treebykooba 0:e8e33b39c3cc 404 strcpy(positionPointer, "oauth_secret\"\r\n\r\n");
treebykooba 0:e8e33b39c3cc 405 positionPointer += sizeof("oauth_secret\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 406 actualsize += sizeof("oauth_secret\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 407 strcpy(positionPointer, m_access_token_secret);
treebykooba 0:e8e33b39c3cc 408 positionPointer += strlen(m_access_token_secret);
treebykooba 0:e8e33b39c3cc 409 actualsize += strlen(m_access_token_secret);
treebykooba 0:e8e33b39c3cc 410 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 411 positionPointer+=2;
treebykooba 0:e8e33b39c3cc 412 actualsize +=2;
treebykooba 0:e8e33b39c3cc 413
treebykooba 0:e8e33b39c3cc 414 //Twitter Message
treebykooba 0:e8e33b39c3cc 415 strcpy(positionPointer, HEADER);
treebykooba 0:e8e33b39c3cc 416 positionPointer += sizeof(HEADER)-1;
treebykooba 0:e8e33b39c3cc 417 actualsize += sizeof(HEADER) -1;
treebykooba 0:e8e33b39c3cc 418 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 419 positionPointer += 2;
treebykooba 0:e8e33b39c3cc 420 actualsize += 2;
treebykooba 0:e8e33b39c3cc 421
treebykooba 0:e8e33b39c3cc 422 strcpy(positionPointer, headerline);
treebykooba 0:e8e33b39c3cc 423 positionPointer += strlen(headerline);
treebykooba 0:e8e33b39c3cc 424 actualsize += strlen(headerline);
treebykooba 0:e8e33b39c3cc 425 strcpy(positionPointer, "message\"\r\n\r\n");
treebykooba 0:e8e33b39c3cc 426 positionPointer += sizeof("message\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 427 actualsize += sizeof("message\"\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 428 strcpy(positionPointer, twitterMessage);
treebykooba 0:e8e33b39c3cc 429 positionPointer += strlen(twitterMessage);
treebykooba 0:e8e33b39c3cc 430 actualsize += strlen(twitterMessage);
treebykooba 0:e8e33b39c3cc 431 strcpy(positionPointer, "\r\n");
treebykooba 0:e8e33b39c3cc 432 positionPointer+=2;
treebykooba 0:e8e33b39c3cc 433 actualsize += 2;
treebykooba 0:e8e33b39c3cc 434
treebykooba 0:e8e33b39c3cc 435 strcpy(positionPointer,HEADER);
treebykooba 0:e8e33b39c3cc 436 positionPointer += sizeof(HEADER)-1;
treebykooba 0:e8e33b39c3cc 437 actualsize += sizeof(HEADER)-1;
treebykooba 0:e8e33b39c3cc 438 strcpy(positionPointer,"\r\nContent-Disposition: form-data; name=\"media\"; filename=\"image.jpg\"\r\n");
treebykooba 0:e8e33b39c3cc 439 positionPointer += sizeof("\r\nContent-Disposition: form-data; name=\"media\"; filename=\"image.jpg\"\r\n")-1;
treebykooba 0:e8e33b39c3cc 440 actualsize += sizeof("\r\nContent-Disposition: form-data; name=\"media\"; filename=\"image.jpg\"\r\n")-1;
treebykooba 0:e8e33b39c3cc 441 strcpy(positionPointer,"Content-Type: image/jpeg\r\n");
treebykooba 0:e8e33b39c3cc 442 positionPointer += sizeof("Content-Type: image/jpeg\r\n")-1;
treebykooba 0:e8e33b39c3cc 443 actualsize += sizeof("Content-Type: image/jpeg\r\n")-1;
treebykooba 0:e8e33b39c3cc 444 strcpy(positionPointer,"Content-Transfer-Encoding: binary\r\n\r\n");
treebykooba 0:e8e33b39c3cc 445 positionPointer += sizeof("Content-Transfer-Encoding: binary\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 446 actualsize+= sizeof("Content-Transfer-Encoding: binary\r\n\r\n")-1;
treebykooba 0:e8e33b39c3cc 447
treebykooba 0:e8e33b39c3cc 448 if (actualsize != neededSize) printf("ERROR in parameters: Calculated size: %i. Actual size: %i.\n", neededSize,actualsize);
treebykooba 0:e8e33b39c3cc 449 writing_position += actualsize;
treebykooba 0:e8e33b39c3cc 450 if (DEBUG) printf("Wrote parameters to buffer.\n");
treebykooba 0:e8e33b39c3cc 451 sentParameters = true;
treebykooba 0:e8e33b39c3cc 452
treebykooba 0:e8e33b39c3cc 453 }
treebykooba 0:e8e33b39c3cc 454 void TwitPic::sendImageData() {
treebykooba 0:e8e33b39c3cc 455 int availableSize;
treebykooba 0:e8e33b39c3cc 456 int amountRead=0;
treebykooba 0:e8e33b39c3cc 457 char* positionPointer;
treebykooba 0:e8e33b39c3cc 458
treebykooba 0:e8e33b39c3cc 459 if (TRACE) printf("-----------------------TRACE: sendImageData\n");
treebykooba 0:e8e33b39c3cc 460
treebykooba 0:e8e33b39c3cc 461 if (writing_position > 0) return;
treebykooba 0:e8e33b39c3cc 462 //fseek(imgFile,-2000,SEEK_END);
treebykooba 0:e8e33b39c3cc 463 positionPointer = sendBuf;
treebykooba 0:e8e33b39c3cc 464 positionPointer += writing_position;
treebykooba 0:e8e33b39c3cc 465 availableSize = SENDING_BUFFER_SIZE - writing_position;
treebykooba 0:e8e33b39c3cc 466 amountRead = fread(positionPointer, 1, availableSize, imgFile);
treebykooba 0:e8e33b39c3cc 467 writing_position += amountRead;
treebykooba 0:e8e33b39c3cc 468 if (DEBUG) printf("Wrote %i bytes of image to buffer.\n",amountRead);
treebykooba 0:e8e33b39c3cc 469 if (feof(imgFile)) {
treebykooba 0:e8e33b39c3cc 470 if (DEBUG) printf("Finished writing file to buffer.\n");
treebykooba 0:e8e33b39c3cc 471 sentImageData = true;
treebykooba 0:e8e33b39c3cc 472 }
treebykooba 0:e8e33b39c3cc 473 }
treebykooba 0:e8e33b39c3cc 474 void TwitPic::sendFooter() {
treebykooba 0:e8e33b39c3cc 475 int availableSize;
treebykooba 0:e8e33b39c3cc 476 int neededSize;
treebykooba 0:e8e33b39c3cc 477 char* positionPointer;
treebykooba 0:e8e33b39c3cc 478
treebykooba 0:e8e33b39c3cc 479 if (TRACE) printf("-----------------------TRACE: sendFooter\n");
treebykooba 0:e8e33b39c3cc 480
treebykooba 0:e8e33b39c3cc 481 availableSize = SENDING_BUFFER_SIZE - writing_position;
treebykooba 0:e8e33b39c3cc 482 neededSize = sizeof("\r\n")-1 +
treebykooba 0:e8e33b39c3cc 483 sizeof(FOOTER) - 1 +
treebykooba 0:e8e33b39c3cc 484 sizeof("\r\n") - 1;
treebykooba 0:e8e33b39c3cc 485 if (availableSize < neededSize) return;
treebykooba 0:e8e33b39c3cc 486
treebykooba 0:e8e33b39c3cc 487 positionPointer = sendBuf;
treebykooba 0:e8e33b39c3cc 488 positionPointer += writing_position;
treebykooba 0:e8e33b39c3cc 489 strcpy(positionPointer,"\r\n");
treebykooba 0:e8e33b39c3cc 490 positionPointer+=2;
treebykooba 0:e8e33b39c3cc 491 strcpy(positionPointer, FOOTER);
treebykooba 0:e8e33b39c3cc 492 positionPointer+=sizeof(FOOTER)-1;
treebykooba 0:e8e33b39c3cc 493 strcpy(positionPointer,"\r\n");
treebykooba 0:e8e33b39c3cc 494 positionPointer+=2;
treebykooba 0:e8e33b39c3cc 495
treebykooba 0:e8e33b39c3cc 496 writing_position+=neededSize;
treebykooba 0:e8e33b39c3cc 497 if (DEBUG) printf("Wrote footer to buffer.\n");
treebykooba 0:e8e33b39c3cc 498 sentFooter=true;
treebykooba 0:e8e33b39c3cc 499 }
treebykooba 0:e8e33b39c3cc 500 void TwitPic::sendNextChunk() {
treebykooba 0:e8e33b39c3cc 501 int amountToSend;
treebykooba 0:e8e33b39c3cc 502 int amountSent;
treebykooba 0:e8e33b39c3cc 503 char* bufPointer;
treebykooba 0:e8e33b39c3cc 504 if (TRACE) printf("-----------------------TRACE: sendNextChunk\n");
treebykooba 0:e8e33b39c3cc 505 //TODO: try both ways of doint this: looped and not.
treebykooba 0:e8e33b39c3cc 506 do {
treebykooba 0:e8e33b39c3cc 507 if (writing_position == 0) {
treebykooba 0:e8e33b39c3cc 508 if (!sentHeaders) {
treebykooba 0:e8e33b39c3cc 509 sendHeaders();
treebykooba 0:e8e33b39c3cc 510 } else if (!sentParameters) {
treebykooba 0:e8e33b39c3cc 511 sendParameters();
treebykooba 0:e8e33b39c3cc 512 } else if (!sentImageData) {
treebykooba 0:e8e33b39c3cc 513 sendImageData();
treebykooba 0:e8e33b39c3cc 514 } else if (!sentFooter) {
treebykooba 0:e8e33b39c3cc 515 sendFooter();
treebykooba 0:e8e33b39c3cc 516 }
treebykooba 0:e8e33b39c3cc 517 }
treebykooba 0:e8e33b39c3cc 518 amountToSend = MIN(writing_position,SENDING_CHUNK_SIZE);
treebykooba 0:e8e33b39c3cc 519 if (sentFooter && amountToSend == 0) {
treebykooba 0:e8e33b39c3cc 520 finishedSending = true;
treebykooba 0:e8e33b39c3cc 521 //getResponse();
treebykooba 0:e8e33b39c3cc 522 }
treebykooba 0:e8e33b39c3cc 523
treebykooba 0:e8e33b39c3cc 524 amountSent = socket.send(sendBuf, amountToSend);
treebykooba 0:e8e33b39c3cc 525 totalSentData += amountSent;
treebykooba 0:e8e33b39c3cc 526
treebykooba 0:e8e33b39c3cc 527 if (DEBUG) printf("---Total sent data: %i bytes\n",totalSentData);
treebykooba 0:e8e33b39c3cc 528
treebykooba 0:e8e33b39c3cc 529 bufPointer = sendBuf + amountSent;
treebykooba 0:e8e33b39c3cc 530 memmove((void*)sendBuf, (void*)bufPointer, writing_position - amountSent);
treebykooba 0:e8e33b39c3cc 531 writing_position -= amountSent;
treebykooba 0:e8e33b39c3cc 532 } while (!finishedSending && amountSent == amountToSend); //if sent less than intended, stop sending until WRITEABLE.
treebykooba 0:e8e33b39c3cc 533 }
treebykooba 0:e8e33b39c3cc 534 void TwitPic::getResponse() {
treebykooba 0:e8e33b39c3cc 535 int amountReceived;
treebykooba 0:e8e33b39c3cc 536 char* okSpot;
treebykooba 0:e8e33b39c3cc 537
treebykooba 0:e8e33b39c3cc 538 if (TRACE) printf("-----------------------TRACE: getResponse\n");
treebykooba 0:e8e33b39c3cc 539 if (DEBUG) printf("Checking for response\n");
treebykooba 0:e8e33b39c3cc 540 amountReceived = socket.recv(recvBuf, RECEIVING_BUFFER_SIZE-1);
treebykooba 0:e8e33b39c3cc 541 recvBuf[amountReceived]='\0';
treebykooba 0:e8e33b39c3cc 542 if (amountReceived > 0 && DEBUG) printf("Received response: %s\n",recvBuf);
treebykooba 0:e8e33b39c3cc 543
treebykooba 0:e8e33b39c3cc 544 okSpot = strstr(recvBuf,"200 OK");
treebykooba 0:e8e33b39c3cc 545 if (okSpot != NULL) {
treebykooba 0:e8e33b39c3cc 546 positiveResponse = true;
treebykooba 0:e8e33b39c3cc 547 readyToReturn = true;
treebykooba 0:e8e33b39c3cc 548 } else if (finishedSending && amountReceived > 0) {
treebykooba 0:e8e33b39c3cc 549 positiveResponse = false;
treebykooba 0:e8e33b39c3cc 550 readyToReturn = true;
treebykooba 0:e8e33b39c3cc 551 }
treebykooba 0:e8e33b39c3cc 552 }
treebykooba 0:e8e33b39c3cc 553
treebykooba 0:e8e33b39c3cc 554 void TwitPic::flushResponse() {
treebykooba 0:e8e33b39c3cc 555 //no longer needed
treebykooba 0:e8e33b39c3cc 556 }