Skip to content

Commit

Permalink
Merge branch 'master' of github.com:roxlu/ofxCurl
Browse files Browse the repository at this point in the history
  • Loading branch information
roxlu committed Sep 5, 2011
2 parents d6a5525 + 1dcc49e commit c6623e8
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 4,455 deletions.
Binary file removed example/mac_xcode/bin/data/test.jpg
Binary file not shown.
1,429 changes: 0 additions & 1,429 deletions example/mac_xcode/curl.xcodeproj/diederick.mode1v3

This file was deleted.

1,896 changes: 0 additions & 1,896 deletions example/mac_xcode/curl.xcodeproj/diederick.pbxuser

This file was deleted.

1,010 changes: 0 additions & 1,010 deletions example/mac_xcode/curl.xcodeproj/project.pbxproj

This file was deleted.

20 changes: 0 additions & 20 deletions example/mac_xcode/openFrameworks-Info.plist

This file was deleted.

16 changes: 0 additions & 16 deletions example/mac_xcode/src/main.cpp

This file was deleted.

58 changes: 0 additions & 58 deletions example/mac_xcode/src/testApp.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions example/mac_xcode/src/testApp.h

This file was deleted.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions example/ofxcurl_upload_file_and_handle_response/testApp.cpp
@@ -0,0 +1,90 @@
#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
ofBackground(33);
}

//--------------------------------------------------------------
void testApp::update(){
}

//--------------------------------------------------------------
void testApp::draw(){

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
if(key == 't') {
// Create a form.
ofxCurl curl;
ofxCurlForm* form = curl.createForm("http://ofxcurl_upload_test.localhost/upload.php");

// Add some form entries.
form->addInput("name", "Diederick Huijbers");
form->addInput("email","diederick_AT_apollomedia.nl");
form->addInput("secret", "call me roxlu");
form->addFile("photo",ofToDataPath("image_to_upload.png",true));

// Perform the post.
try {
form->post();
}
catch(...) {
cout << "OOPS.. something went wrong while posting" << endl;
}

// Do something with the response from the post.
vector<char> response_buf = form->getPostResponseAsBuffer();
string response_str = form->getPostResponseAsString();
cout << "Size of response buffer: " << response_buf.size() << endl << endl;
cout << "Response string:" << endl;
cout << "-----------------" << endl;
cout << response_str <<endl;

// Cleanup
delete form;
}

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}
1 change: 1 addition & 0 deletions example/php/log.txt
@@ -0,0 +1 @@

30 changes: 30 additions & 0 deletions example/php/upload.php
@@ -0,0 +1,30 @@
<?php
/**
* What follows is a very very basic upload handler. This is just an example
* how one can handle uploaded files. I created this tiny script to test the
* file upload of ofxCurlForm and handling of the response.
*
*
* @author Diederick Huijbers <diederick_AT_apollomedia.nl>
* @date 2011.07.20
*/
function l($s) {
file_put_contents('log.txt', date('Y-m-d',time()) ."\t" .$s ."\n", FILE_APPEND);
}
function e($s) {
echo $s ."<br>";
}

$upload_dir = dirname(__FILE__) .'/uploads/';
$upload_path = $upload_dir .$_FILES['photo']['name'];
if(move_uploaded_file($_FILES['photo']['tmp_name'], $upload_path)) {
l("photo: ". basename( $_FILES['photo']['name']));
} else{
l('photo: cannot upload');
}
l('name: ' .$_POST['name']);
l('email: ' .$_POST['email']);
l('secret: ' .$_POST['secret']);
l('------------------');
echo 'name=' .$_POST['name'] ."\n";

Binary file added example/php/uploads/image_to_upload.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions src/ofxCurlForm.cpp
Expand Up @@ -4,6 +4,21 @@
#include "ofxCurlFormTypeFile.h"

#include <iostream>

// copy response data of form post into buffer.
size_t ofxcurl_post_write_function(char* data, size_t data_size, size_t num_data_size_entries, void* userdata) {

size_t real_num_bytes = data_size * num_data_size_entries;
ofxCurlForm* form = static_cast<ofxCurlForm*>(userdata);
std::copy(
data
,data+real_num_bytes
,std::back_inserter(form->post_response_buffer)
);

return real_num_bytes;
}

ofxCurlForm::ofxCurlForm(std::string sPostURL)
:action(sPostURL)
,post_curr(NULL)
Expand Down Expand Up @@ -55,6 +70,8 @@ ofxCurlForm& ofxCurlForm::post() {
curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT_MS, timeout_after_ms);
curl_easy_setopt(easy_handle, CURLOPT_HTTPPOST, post_curr);
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, ofxcurl_post_write_function);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, this);
result = curl_easy_perform(easy_handle);

// free list..
Expand Down Expand Up @@ -93,3 +110,12 @@ void ofxCurlForm::cleanup() {
it = elements.erase(it);
};
}

std::vector<char> ofxCurlForm::getPostResponseAsBuffer() {
return post_response_buffer;
}
std::string ofxCurlForm::getPostResponseAsString() {
std::string result;
std::copy(post_response_buffer.begin(), post_response_buffer.end(), std::back_inserter(result));
return result;
}
7 changes: 7 additions & 0 deletions src/ofxCurlForm.h
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <vector>
#include "curl.h"

#include "ofxCurlException.h"
class ofxCurlFormType;

Expand All @@ -16,11 +17,17 @@ class ofxCurlForm {
ofxCurlForm& addFile(std::string sName, std::string sFilePath);
ofxCurlForm& post();
ofxCurlForm& setTimeout(int nTimeoutMillis);

std::vector<char> getPostResponseAsBuffer();
std::string getPostResponseAsString();

void cleanup(); // removes all elements and frees memory.
std::vector<char> post_response_buffer;
private:
void parseForm();
std::string action;
std::vector<ofxCurlFormType*> elements;

int timeout_after_ms;
struct curl_httppost* post_curr;
struct curl_httppost* post_last;
Expand Down
4 changes: 2 additions & 2 deletions src/ofxCurlFormType.h
Expand Up @@ -14,8 +14,8 @@ class ofxCurlFormType {
//--------------------------------------------------------------------------
virtual void addToForm(
ofxCurlForm* pForm
,curl_httppost** pCurr
,curl_httppost** pLast
,curl_httppost** pCurr // used to keep track of order of added elements
,curl_httppost** pLast // used to keep track of order of added elements
) = 0;

protected:
Expand Down

0 comments on commit c6623e8

Please sign in to comment.